Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array of pointers multidimensional?

Status
Not open for further replies.

aznluvsmc

MIS
Mar 21, 2004
476
CA
I was just wondering if creating an array of pointers which each point to a string considered a multidimensional array?


I'm new to C and just learning it so I'm sorry if this question seems stupid. Thanks.
 
for technical questions we refer you to marsd,
but in my opinion I am sure it would amount to
the same thing.

you shall hear shortly the pros and cons.

TC
 
The following are all logically equvilent.
Code:
char * blah[];
char ** blah;
char blah[][];
as evidenced by main's second parameter:
Code:
int main(int argc, [green]char** argv[/green])
int main(int argc, [green]char * argv[][/green])
int main(int argc, [green]char argv[][][/green])

[plug=shameless]
[/plug]
 
If you need to dynamically allocate the multidimensional arrays, try:

Code:
#include <memory.h>
#include <string.h>
#define DIM_ONE_SIZE 10
#define DIM_TWO_SIZE 256

int main(){

   int i;

   //Allocate dimension one
   char** multiDimArray = 
      calloc (DIM_ONE_SIZE, sizeof(char*));

   //Allocate dimension two
   for(i=0; i<DIM_ONE_SIZE; i++)
     multiDimArray[i] = calloc (DIM_TWO_SIZE, sizeof(char));
   
   //Assign cells
   strcpy(multiDimArray[0], "hell");
   strcpy(multiDimArray[1], "goodbye");
   multiDimArray[0][4] = 'o';
   multiDimArray[0][5] = '\0';

   //Print cells
   printf("%s\n", multiDimArray[0]);
   printf("%s\n", multiDimArray[1]);


   //Free heap memory 
   for(i=0; i<DIM_ONE_SIZE; i++)
     free(multiDimArray[i]);	  // free pointers to     
                                  // strings.

   free(multiDimArray);		  // free pointer to array 
                                  // of (now deallocated) 
                                  // pointers. 

   return 0;
}


>  hello
>  goodbye
 
Thanks for the help. I see clearly now that an array of pointers is multidimensional.
 
Well, they may be functionally equivalent, but they are different. The way the storage is allocated for each can be very different.

Using RealityVelJackson's example, it could also be written this way...
Code:
#include <memory.h>
#include <string.h>

#define DIM_ONE_SIZE 10
#define DIM_TWO_SIZE 256

int main(){

   int i;

   char    multiDimArray[DIM_ONE_SIZE][DIM_TWO_SIZE];

   //Assign cells
   strcpy(multiDimArray[0], "hell");
   strcpy(multiDimArray[1], "goodbye");
   multiDimArray[0][4] = 'o';
   multiDimArray[0][5] = '\0';

   //Print cells
   printf("%s\n", multiDimArray[0]);
   printf("%s\n", multiDimArray[1]);

   return 0;
}
The results are the exact same, but the way the memory is structured and aranged is VERY different.

Hope this helps.
 
I guess we could say a true multi dim array is
a single block of memory and the array of pointers
to char string is most probably fragmented in memory.

TC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top