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

Replies

  • anandkumarjha
    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?
  • xxxabhash007
    xxxabhash007
    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
    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
    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
    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
    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
    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
    thank you silverscorpion...now it cleared my doubt...

You are reading an archived discussion.

Related Posts

I've been evaluating Samsung Wave S8500. The phone packs 1500mAh battery. I charged the phone to 100% and put the phone on regular use [ non-3G network, moderate GPRS use...
Suggest some technical events that can be organised in any inauguration
hello friends, I write a program to copy a text file in java. But i didn't get the text in serial way. Check out this import java.io.*; public class r...
hi, i wana know about the "best simulator" for PLC ladder programming... Plz help me to find this.....
can any one suggest me some projects on networking based on ccna