Assigning memory to double pointer?

后端 未结 7 882
广开言路
广开言路 2020-12-04 16:18

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(\         


        
相关标签:
7条回答
  • 2020-12-04 17:00

    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];
    }
    
    0 讨论(0)
提交回复
热议问题