Warning

 

Close
Confirm Action

Are you sure you wish to do this?

Cancel Confirm
AR15.COM
11/15/2013 9:14:59 AM EDT
I need to use an array in a function called by main but Id like to first do some processing in main to determine how large to make the array. So the following works, but wont compile if I move the foo declaration into main:

#include <iostream>

using namespace std;
int foo[]={0,1};
void bar();
int main() {
    bar();
   return 0;
}
void bar() {
   cout << foo[0] << endl;
}
11/15/2013 9:21:51 AM EDT
[#1]
When foo is declared outside of main(), it's within the file's scope. That makes it accessible to every function within that file.



If it's declared within main, the variable's scope is limited to main(), so it isn't defined in foo().



You either need to keep it in the file scope, or define it in main() and pass it as an argument to foo().
11/15/2013 9:26:54 AM EDT
[#2]
Quote History
Quoted:
When foo is declared outside of main(), it's within the file's scope. That makes it accessible to every function within that file.

If it's declared within main, the variable's scope is limited to main(), so it isn't defined in foo().

You either need to keep it in the file scope, or define it in main() and pass it as an argument to foo().
View Quote


yes thats what I thought, thanks.  my issue is foo gets recursed on millions of times and my array is fairly large and its prob a bad idea to do that.  oh well thanks
11/15/2013 9:34:11 AM EDT
[#3]

Quote History
Quoted:
yes thats what I thought, thanks.  my issue is foo gets recursed on millions of times and my array is fairly large and its prob a bad idea to do that.  oh well thanks
View Quote View All Quotes
View All Quotes
Quote History
Quoted:



Quoted:

When foo is declared outside of main(), it's within the file's scope. That makes it accessible to every function within that file.



If it's declared within main, the variable's scope is limited to main(), so it isn't defined in foo().



You either need to keep it in the file scope, or define it in main() and pass it as an argument to foo().




yes thats what I thought, thanks.  my issue is foo gets recursed on millions of times and my array is fairly large and its prob a bad idea to do that.  oh well thanks


A bad idea to do what? If it's in the file scope, no problem. Passing the array to foo() will just pass a pointer.



 
11/15/2013 9:39:27 AM EDT
[#4]
Quote History
Quoted:

A bad idea to do what? If it's in the file scope, no problem. Passing the array to foo() will just pass a pointer.
 
View Quote View All Quotes
View All Quotes
Quote History
Quoted:
Quoted:
Quoted:
When foo is declared outside of main(), it's within the file's scope. That makes it accessible to every function within that file.

If it's declared within main, the variable's scope is limited to main(), so it isn't defined in foo().

You either need to keep it in the file scope, or define it in main() and pass it as an argument to foo().


yes thats what I thought, thanks.  my issue is foo gets recursed on millions of times and my array is fairly large and its prob a bad idea to do that.  oh well thanks

A bad idea to do what? If it's in the file scope, no problem. Passing the array to foo() will just pass a pointer.
 


suddenly I understand the usefulness of dynamic memory and pointers
11/15/2013 9:41:51 AM EDT
[#5]
And I just realized I've been saying foo() instead of bar(). But you get the idea.
11/15/2013 2:24:54 PM EDT
[#6]
thanks for all the help...ok ive done some dorking around and have been able to pass a single dimension array (inherently dereferenced to a pointer) to a function that is expecting a pointer and can manipulate the array in the function.  great.  but now I need to do it 2D.  I've done some looking around and all I can see is stuff like this here where you have to know the size at compile time.  what if you dont?
11/15/2013 3:44:09 PM EDT
[#7]
The only sane way to pass dynamic two dimensional arrays as arguments is using pointers.

void Foo( int** pArray ); //managing this memory is annoying,

or using a 1 dimensional array as two

void Foo( int* pArray ) { ... pArray[ x * COLS + y ] = ... }

but this is C++, we have vectors


#include <vector>

void Foo( std::vector<int>& data );
void Bar( int v );

#define ROWS 10
#define COLS 20

int main( void )
{
//allocate vector all at once
std::vector<int> myVec( ROWS * COLS );

Foo( myVec );

//no need for cleanup, vector handles that for us
return 0;
}


void Foo( std::vector<int>& data )
{
//iterate through all elements in 2D fashion
for( int i = 0; i < ROWS; i++ )
for( int j = 0; j < COLS; j++ )
{
data[ i * COLS + j ] = i + j; //the vector class overloads [], so you can use them just like normal arrays
}
   
//you can also get a pointer to the elements
int* p = &data[0];

for( int i = 0; i < ROWS; i++ )
for( int j = 0; j < COLS; j++ )
{
Bar( p[ i * COLS + j ] );
}

//or just iterate linearly
std::vector<int>::iterator it = data.begin();
std::vector<int>::iterator end = data.end();
for( ; it != end; it++ )
{
Bar( *it );
}
}

void Bar( int v )
{
//do stuff with values
}
11/17/2013 9:02:53 PM EDT
[#8]
Quote History
Quoted:


yes thats what I thought, thanks.  my issue is foo gets recursed on millions of times and my array is fairly large and its prob a bad idea to do that.  oh well thanks
View Quote View All Quotes
View All Quotes
Quote History
Quoted:
Quoted:
When foo is declared outside of main(), it's within the file's scope. That makes it accessible to every function within that file.

If it's declared within main, the variable's scope is limited to main(), so it isn't defined in foo().

You either need to keep it in the file scope, or define it in main() and pass it as an argument to foo().


yes thats what I thought, thanks.  my issue is foo gets recursed on millions of times and my array is fairly large and its prob a bad idea to do that.  oh well thanks



Bear in mind that every set of { } creates a new scope. Nested { } have access to the higher level variable within those nested braces, but not vice versa and not to braces at an equal level.

{  int b;
  {
     int c = 2;
     b = 1 + c;
  }
  c = 6; // no, c is gone, gone, gone once we passed } from the scope where it was declared
}

{
  b = 2; // no, b is not accessible here.
}