views:

536

answers:

3

I am having trouble understanding how to assign memory to a double pointer. I want to read an array of strings and store it.

 char **ptr;
    fp=fopen("file.txt","r");
    ptr=(char**)malloc(sizeof(char*)*50);
    for(int i=0;i<20;i++)
    {
       ptr[i]=(char*)malloc(sizeof(char)*50);
       fgets(ptr[i],50,fp);
    }

instead of this of i just assign a large block of memory and store the string

char **ptr;
  ptr=(char**)malloc(sizeof(char)*50*50);

would that be wrong? And if so why is it?

A: 

A double pointer is just a pointer to another pointer. So you can allocate it like this:

char *realptr=(char*)malloc(1234);
char **ptr=&realptr;

You have to keep in mind where your pointer is stored at (in this example the double pointer points to a pointer variable on the stack so it's invalid after the function returns).

Pent Ploompuu
A: 

Double pointer is, simply put, a pointer to a pointer, In many cases it is used as an array of other types.

For example, if you want to create an array of strings you can simply do:

char** stringArray = calloc(10, 40);

this will create an array of size 10, each element will be a string of length 40.

thus you can access this by stringArray[5] and get a string in the 6th position.

this is one usage, the others are as mentioned above, a pointer to a pointer, and can be allocated simply by:

char* str = (char*)malloc(40);
char** pointerToPointer = &str //Get the address of the str pointer, valid only in the current closure.

read more here: good array tutorial

MindFold
No, this creates a single array of 100 char*, assuming char* takes 4 bytes.
Hans Passant
+3  A: 

Your second example is wrong because each memory location conceptually would not hold a char* but rather a char. If you slightly change your thinking, it can help with this:

char *x;  // Memory locations pointed to by x contain 'char'
char **y; // Memory locations pointed to by y contain 'char*'

x = (char*)malloc(sizeof(char) * 100);   // 100 'char'
y = (char**)malloc(sizeof(char*) * 100); // 100 'char*'

// below is incorrect:
y = (char**)malloc(sizeof(char) * 50 * 50);
// 2500 'char' not 50 'char*' pointing to 50 'char'

Because of that, your first loop would be how you do in C an array of character arrays/pointers. Using a fixed block of memory for an array of character arrays is ok, but you would use a single char* rather than a char**, since you would not have any pointers in the memory, just chars.

char *x = calloc(50 * 50, sizeof(char));

for (ii = 0; ii < 50; ++ii) {
    // Note that each string is just an OFFSET into the memory block
    // You must be sensitive to this when using these 'strings'
    char *str = x[ii * 50];
}
sixlettervariables