Declaring an array:
int * myGreatArray[50];
This is an array that stores 50 pointers to int. Be aware that it does not allocate the storage for those integers, just for the storage of the pointers.
int arr[50]; //array of 50 integers
int * parr = arr; /*pointer to an int, which may be
the beginning of an array*/
Passing to a function:
This is exactly what I was hoping I'd discover, so when passing an array into a function, is it more efficient to pass a pointer to an array as opposed to an array of pointers? I'd think yes. – Michael Hackman
The two function definitions:
void doStuffToArray(int ** array, size_t len)
{
//dostuff
}
and
void doStuffToArray(int * array[], size_t len)
{
//dostuff
}
are functionally identical. When you pass an array, the function actually receives a pointer to the array.
To call the functions, you can pass the array (devolves to pointer to the beginning of the array, (recommended) or a pointer to the beginning of the array (not recommended for full arrays, but is useful to pass pointers to sections of arrays):
int arr[10] = {};
doStuffToArray(arr, sizeof(arr)/sizeof(arr[0])); //functionally identical
doStuffToArray(&arr[0], sizeof(arr)/sizeof(arr[0])); //functionally identical
When passing an array of pointers, there are two function definitions that can be used, e.g. argv is an array of pointers to char arrays:
int main(int argc, char * argv[]){return 0;} //functionally identical
int main(int argc, char ** argv ){return 0;} //functionally identical
My advice is to use the array notation (with the []) as this is a declaration of intent, instead of the equivalent but more ambiguous pointer notation.
If you know how big the array is, then argv could have been defined as an 'array of arrays' char argv[][] which would be great, but can't be done. When defining a function, only the first array dimension can be undefined, any further dimensions have to be defined. If you know how big it is though, there is nothing to stop you from creating a function:
void doStuffToMyArray( int array[][10]){
/*...*/
}
int**is not a pointer to an array. It is a pointer to a pointer to anint. BTW, in themalloc, you meansizeof(int*)instead ofsizeof(int), right?