View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12933 Members
Join this group to post and comment.
anandkumarjha
anandkumarjha • Aug 28, 2010

Pointers at a glance

[FONT="Garamond"[COLOR="yellow"][/COLOR]]The difference between constant pointer and the pointer to a constant[/FONT]
Absolutely there is a vast difference btween constant pointers and pointer to the constant.it will be more clear through the examples of each types
constant pointer:-(int const *ptr):- this constant pointer ptr points to the variable and can not be modified or change further,it will point to that variable permanently but in case of pointer to a constant(const int *ptr):-in this case pointer pointes to the constant variable and that variable can not be changed or modified further
anandkumarjha
anandkumarjha • Aug 28, 2010
char ch[]="ready";
char *ch="ready;
what's the difference between these two statements and which one should be prefered in the real time world?
xxxabhash007
xxxabhash007 • Aug 28, 2010
When the compiler sees the expression ch[3], it emits code to the start of the location "ch", move three past it, and fetch the character there.
When it sees the expression pointer ch[3], it emits code to start at the location "ch", fetch the pointer value there, add three to the pointer, and finally fetch the character pointed to, which can have different character other than expected.
Thus both of the given expressions are entirely different and thus we can't use them in the same sense.
silverscorpion
silverscorpion • Aug 29, 2010
anandkumarjha
char ch[]="ready";
char *ch="ready;
what's the difference between these two statements and which one should be prefered in the real time world?

In the first statement, ie., char ch[]="ready",
ch is a character array. The compiler allocates 5 bytes of memory to the variable ch, and stores the string "ready" in it.

In the second statement, the variable ch is just a character pointer.
ie., the compiler stores the string "ready" in some continuous memory locations, and makes the pointer ch point to the first byte of the string.
So, ch points to the character 'r'.

This is the difference..
anandkumarjha
anandkumarjha • Aug 29, 2010
thank you silverscorpion for your answer but it again bothers me that then how can we access all the characters in an array with the help of pointer variables?
anandkumarjha
anandkumarjha • Aug 29, 2010
hello silverscorpion,
i abide by your answer but from somewhere i learnt that array is itself the pointer to the first variable of the array i.e in char ch[]="read" character ch points to the variable r and same is the function with (char *ch=read).so what's the difference between these two statements?
silverscorpion
silverscorpion • Aug 29, 2010
Ok...

char ch[]="ready"
char *ch="ready"

As I already told, in 1st statement, ch is an array. In 2nd, ch is a char pointer.

What this means is that, the size of ch in 1st statement is essentially the size of the string stored in it, which in this case is 5 bytes.
In 2nd statement, the size of ch is just two bytes (or the size of an integer), because it's just a char pointer.

And yes, the name of an array can also be used as a pointer to the first element.
So, in your case, in 1st statement, using only 'ch' without any index will return the character 'r'.

when you reference an array element by ch, what you actually do is, move i memory locations from the first location.
So, ch is equal to *(ch + i), where ch is the address of the first element.

Am I clear? Does this answer your question??
xxxabhash007
xxxabhash007 • Aug 29, 2010
An array is a block of continuous memory which holds the value whereas pointer just points to the first character of the variable "ready".
anandkumarjha
anandkumarjha • Aug 29, 2010
thank you silverscorpion...now it cleared my doubt...

Share this content on your social channels -