开发者

c pointer assignment to pointer to pointer what does it mean

I have a code snippet am struggling to understand.

char *c;   // c is uni dimensional table ( single row )

char **p ; // p is a two dimensional table 

**p = *c;  // what does this mean ?

When I do the above the assignment, is the c copied as first row of p ?

or c is copied as first c开发者_Python百科olumn of p ?


**p = *c; // what does this mean ?

When I do the above the assignment , Is the c copied as first row of p ? or c is copied as first column of p ?

Neither, that code is copying the first element of c to the first element of p. Is equivalent to

p[0][0] = c[0];


char *c;   // c is uni dimensional table ( single row )

No, c is a pointer, not an array. If you initialize it properly, it can point to the first element of an array, but the pointer itself is not an array.

char **p ; // p is a two dimensional table 

No, p is a pointer to a char*; it's not a table. Again, it might point to something that acts like a two-dimensional array. A true two-dimensional array is simply an array of arrays, but there are several other ways to use pointers to simulate more flexible versions of 2-d arrays, with dynamic allocation and varying row sizes.

**p = *c;  // what does this mean ?

If p and c haven't been initialized, it means undefined behavior (which means your program crashes if you're lucky. If they have been initialized properly: p points to a char* object; let's call that object pstar. pstar points to a char object; let's call that object pstarstar. c also points to a char object; let's call it cstar. The assignment copies the value of cstar into pstarstar.

What that means depends on what p and c point to.

Recommended reading: section 6 of the comp.lang.c FAQ.


It means that the char that c points to is copied to whatever p points to points to.

c->some char

is copied to

p->*p->some char
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜