Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C allocated pointers? What's this?

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?

like image 779
Pithikos Avatar asked Dec 01 '11 11:12

Pithikos


1 Answers

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.

like image 142
Graham Borland Avatar answered Oct 16 '22 13:10

Graham Borland