Hello all,
I am writing a program that requires a global array of pointers to char:
Code:
char *target_mac[ 5 ];
I'm a little confused about exactly how to allocate memory for this--I think I have over-thought it and psyched myself out.
Unless I am really messed up, I do not need to allocate memory to the array itself: Since it is an array of pointers, that is taken care of with the [5] in the declaration, i.e., I get five pointers to char with that. Right? And the allocation for each element does not matter at this point, because a pointer is just a memory location, and I've given myself five here.
Each element needs to have memory allocated to it though...like this:
Code:
for( x = 0; x < 5; x++ ) {
target_mac[ x ] = ( char * ) malloc( MAX_SIZE_ETHADDR + 1 );
}
I assume the above allocation can be coded in main( ) and later free( )'d when I am done with it, but again, I think I have psyched myself out. Perhaps a little diagram of the flow of the array usage might help:
Code:
+main() // allocate the memory to the array elements, pass ipAddr
|
+--+getMac( ipAddr ) passes ipAddr to otherFunction( )
|
+--+otherFunction( ipAddr ) // doesn't touch target_mac[ ]
|
+--+
|
+--+getMac( ) // now packs target_mac[ ] with info from otherFunction
|
+main() // uses info in target_mac array and then frees it
Hope that makes sense, and everyone can see what I'm asking here.
TIA.