Sign in to follow this  

What is the difference between char* and char**

This topic is 3504 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi there, Can somebody please help explain to me the main differences between the below two (2) declarations? 1. char * str = (char*)malloc(5 * sizeof(char*)); 2. char ** str = (char**)malloc(5 * sizeof(char**)); Thanks, Jr

Share this post


Link to post
Share on other sites
char* is a pointer to a character in memory, char** is a pointer to another pointer that points to a character in memory.


1. would declare an array of 5 characters
2. would declare an array of 5 pointers to characters

Share this post


Link to post
Share on other sites
Actually, they both seem wrong. The first one doesn't seem to allocate 5 characters (which is usually the intention), but a number of characters that are equal to the size of five char pointers (about 20 characters?). Same for the second.

char* p = (char*)malloc(5 * sizeof(char));
char** q = (char**)malloc(5 * sizeof(char*));

Now the first one would point to memory that can hold a string up to 4 characters long (plus the null-terminator).
The second one points to an array of strings (which haven't been allocated yet).

Share this post


Link to post
Share on other sites
Quote:
Original post by visitor
Actually, they both seem wrong. The first one doesn't seem to allocate 5 characters (which is usually the intention), but a number of characters that are equal to the size of five char pointers (about 20 characters?). Same for the second.

char* p = (char*)malloc(5 * sizeof(char));
char** q = (char**)malloc(5 * sizeof(char*));

Now the first one would point to memory that can hold a string up to 4 characters long (plus the null-terminator).
The second one points to an array of strings (which haven't been allocated yet).

And if this is C and not C++ then casting the return of malloc is dangerous.

Share this post


Link to post
Share on other sites

char a;
char *b;
char **c;

a is a variable that contains a character.
b is a variable that contains the address of a variable containing a character.
c is a variable that contains the address of a variable containing the address of a variable containing a character.

Share this post


Link to post
Share on other sites
don't get confused about the idea of pointers

a pointer is a term used to denote a variable which has a value denoting a memory address.

for example:
int n = 10;
it's a variable of type integer having value of 10

int* pn = &n;
pn is a variable holding the address of variable n, for example 1000
an address is just an integer variable (usually written using hex numbers)

pn is just like any another variable, you can increment it and decrement it
beside these ordinary operations there are some special ones which can only be applied to pointers (this means if you try to do them to other variables compiler will give you an error)

the operator * is for deferencing the memory address, that is reading the value at the memory address hold by the pointer variable

*pn returns the value stored at the address hold by pn
since pn was assigned previously to the address of variable n, and since n was assigned to 10, this means *pn returns 10

one thing to remember about increment\decrement pointers:
suppose pn has the vale of 1000 (that means it holds the address 1000)
pn = pn + 1;
if pn was 1000 now is 1004. why ? because when you increment pointers the value of the pointer is increment by the size of the type it points to

sizeof(int) = 4 (int is on 4 bytes on 32 bits platform)

pn = pn + 3
if pn was 1004 now its 1016 ( 1004 + 4 + 4 + 4)

now, you can have:

int** ppn = &pn;
this means:
pointer to pointer to int
or else said:
variable having as value a memory address of a variable which in turn also has as value a memory address of another variable

int this case, ppn has as value the memory address of pn which in turn has as value the memory address of variable n

so let's suppose we have this:
n = 10; // value of 10
pn = 1000; // value of 1000 which is the address of variable n
ppn = 1500; // value of 1500 which is the address of variable pn

so:

*ppn = 1000; //
*pn = 10;


i will let you see what's the value of ppn after:
ppn = ppn + 1;

:D



Share this post


Link to post
Share on other sites

This topic is 3504 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this