English 中文(简体)
Malloc a pointer to a pointer to a structure array by reference
原标题:

The code below compiles, but immediately crashes for reasons obvious to others, but not to me. I can t seem to get it right, can anyone tell me how to fix this.

*array_ref[2] = array[0];
*array_ref[3] = array[1];

It crashes on that part everytime.

typedef struct test {
    char *name;
    char *last_name;
} person;



int setName(person ** array, person ***array_ref) {

    *array = malloc (5 * sizeof(person));
    *array_ref= malloc(5 * sizeof(person*));

   array[0]->name = strdup("Bob");
   array[1]->name = strdup("Joseph");
   array[0]->last_name = strdup("Robert");
   array[1]->last_name = strdup("Clark");


*array_ref[2] = array[0];
*array_ref[3] = array[1];


    return 1;
}



int main()
{
    person *array;
    person **array_r;

   setName(&array,&array_r);

    printf("First name is %s %s
", array[0].name, array[0].last_name);
    printf("Second name is %s %s
", array_r[3]->name, array_r[3]->last_name);

     while(1) {}
    return 0;
}
最佳回答

Operator [] has higher precedence than unary operator*. Hence, this:

*array_ref[2] = array[0];
*array_ref[3] = array[1];

actually means:

*(array_ref[2]) = array[0];
*(array_ref[3]) = array[1];

Types are correct here, which is why it compiles. But from your code it s clear that your intent actually was:

(*array_ref)[2] = array[0];
(*array_ref)[3] = array[1];

So just use parentheses.

问题回答

You allocated space for the array_ref pointers, but not for what they point to.

Try changing the following in setName()

 *array_ref[2] = array[0];
 *array_ref[3] = array[1];

to

*(*array_ref+2) = array[0];
*(*array_ref+3) = array[1];

This works.

array[1]->name is your problem. This should be (*array)[1].name. Notice how the two aren t equivalent. All the similar uses have the same problem, except for [0], which accidentally does the right thing.

Remember that array, the function parameter, isn t your array, it s a pointer to your array.

In functions like this I prefer code like:

int setName(person ** out_array, person ***out_array_ref) {
    person* array = malloc(5 * sizeof(person));
    person** array_ref = malloc(5 * sizeof(person*));
    array[0].name = strdup("Bob");
    array[1].name = strdup("Joseph");
    array[0].last_name = strdup("Robert");
    array[1].last_name = strdup("Clark");
    // I m guessing this was your intent for array_ref, here:
    array_ref[2] = &array[0];
    array_ref[3] = &array[1];

    *out_array = out_array;
    *out_array_ref = array_ref;
    return 1;
}

Note that this catches both array[1]->name as noted by Roger Pate, and *array_ref[2] = array[0] as (almost) noted by Pavel - whose solution (*array_ref)[2] = array[0] assigns from an unallocated person* array[1] - both of which are hard to notice with the extra dereference.

Of course, I mostly do this because I use C++, and this increases exception safety ;).





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签