Valentin Maerten
Valentin Maerten

Reputation: 3

C - Pointer - Am i understanding well?

I'm trying to understand this type of variable: char**.

For me, char** is a pointer which is pointing to an address where I can find an array of pointers of type char* which are pointing to a addresses where I can found my string. Am I right?

Upvotes: 0

Views: 113

Answers (5)

Sathish
Sathish

Reputation: 3870

char** -> is pointer to pointer to a character. char** is a pointer to a char *, where char * may pointing to an array of character or can point to a character or may not!

when you allocate memory like this-

char **ptr;
int i;
ptr=(char **)malloc(2*sizeof(char *));
for(i=0;i<2;i++)
ptr=(char *)malloc(10*sizeof(char));

where ptr is a array of 2 char *, and each char * is pointing to array of 10 characters or string!

Upvotes: 0

GSPdibbler
GSPdibbler

Reputation: 405

Not exactly. char** may point to an address where you can find a pointer of type char*, not necessarily an array. char* is pointing to an address where you can find variable of type char, not string.

Upvotes: 0

S.C. Madsen
S.C. Madsen

Reputation: 5256

Please try This cute little site, and you will never have trouble understanding pointer-syntax again :-)

NOTE: I am not affilatied with cdecl.org in any way.

Upvotes: 1

Quentin
Quentin

Reputation: 63144

Almost. char** by itself doesn't ensure that it's pointing to an array of char*. It may, or may not, depending on what your program does. Following the same logic, a char* often points to a string (a character array), but not necessarily.

Upvotes: 4

ars
ars

Reputation: 707

char** - is a pointer, which points to a pointer, which CAN point to a array, can point to a single variable, and can point to nothing.

Usually, two dimensional pointers useful with dynamic arrays, you can use like this

...
Point** SizeArray = new Point*[RowCount];
SizeArray[RowIndex] = new Point[ColCount];
...
SizeArray[CurrentRowIndex][CurrentColIndex] = Point(x,y);
...
for (UInt32 i = 0; i < RowCount; ++i)
   delete SizeArray[i]; // deletes an inner array of Points;
delete SizeArray;
...

Upvotes: 1

Related Questions