Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ arrays as pointers question

Why this is possible:

char buf[10], *pbuf = buf, **ppbuf = &pbuf;

and this isn't:

char buf[10], **ppbuf = &buf;

As I understand, the second line is just a shorthand of the first one.

like image 683
dimayak Avatar asked Dec 13 '10 12:12

dimayak


1 Answers

They're not equivalent.

*pbuf = buf

That means, "pbuf is a pointer to type char, whose value is the address of buf." Since buf is an array of chars, this works.

**ppbuf = &pbuf

That means, "ppbuf is a pointer to a pointer to type char, whose value is the address of pbuf." Since pbuf is a pointer to type char, this works.

**ppbuf = &buf

This means, "ppbuf is a pointer to a pointer to type char, whose value is the address of buf." Since buf is an array of chars, this fails.

like image 132
Dan Breslau Avatar answered Sep 20 '22 22:09

Dan Breslau