Posted: 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> |
|
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(). |
|
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 |
|
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 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. |
|
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. 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 |
| 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? |
|
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
|
|
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 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.
|
But you get the idea.