I have the below code.
char a[] = "abcde";
char *b = "fghij";
char *c = malloc(6);
char *d = c;
c = "klmno";
And the exercise states:
Draw a picture of the data structures a, b, c and d(with content), where you can see what has been allocated and use arrows to show how pointers are set.
My solution is:
____________
a -> |a|b|c|d|e|\0|
¨¨¨¨¨¨¨¨¨¨¨¨
____________
b -> |f|g|h|i|j|\0|
¨¨¨¨¨¨¨¨¨¨¨¨
____________
c -> |k|l|m|n|o|\0|
¨¨¨¨¨¨¨¨¨¨¨¨
___________
d -> | | | | | | |
¨¨¨¨¨¨¨¨¨¨¨
However my solution did not get accepted and the response was "allocated memory for a pointer to b, c, d but not a". Can someone explain me what this means?
It's a slightly cryptic response, but I guess the complaint is that the array a
doesn't point to that data; it contains the data.
So this might be what is required (without the pointer arrow):
____________
a |a|b|c|d|e|\0|
¨¨¨¨¨¨¨¨¨¨¨¨
The name of an array can be used as a pointer, but it still has a subtly different meaning.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With