CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: const pointers and pointer to const

    const applies to what is on its immediate left, if nothing on the left then on the right.
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  2. #2
    Join Date
    Jul 2005
    Posts
    121

    Question const pointers and pointer to const

    i'm getting confused in understanding the use of them
    i've read the meaning of them more than 20 times but it couldn't find a way to my brain
    i couldn't figure out what is the value that can't not change and what is not

    example
    [code]
    int a=12;
    const int b=2;
    int c=4;
    const int *p;
    int *const pp=&a;
    [\code]

    which is wrong and which is legal
    Code:
    p=&b;
    *p=33;
    pp=&c; // if this is legal then why the pointer is const if it's value changed
    pointers to const : if the address of the pointer shouldn't change then what is the difference between it and constant pointers
    couse in the two ways i can't modify the value

    i hope if you give me another simple clear example and not give me meanings or syntax declarations

  3. #3
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: const pointers and pointer to const

    Last edited by Vedam Shashank; September 22nd, 2005 at 04:43 AM.
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Thumbs up Re: const pointers and pointer to const

    Just go through this example.

    Pointer to const
    The trick with a pointer definition, as with any complicated definition, is to
    read it starting at the identifier and work your way out. The const specifier
    binds to the thing it is “closest to.” So if you want to prevent any changes
    to the element you are pointing to, you write a definition like this:


    const int* u;

    Starting from the identifier, we read “u is a pointer, which points to a const
    int.” Here, no initialization is required because you’re saying that u can
    point to anything (that is, it is not const), but the thing it points to cannot
    be changed.


    Here’s the mildly confusing part. You might think that to make the pointer
    itself unchangeable, that is, to prevent any change to the address
    contained inside u, you would simply move the const to the other side of
    the int like this:


    int const* v;

    It’s not all that crazy to think that this should read “v is a const pointer to
    an int.” However, the way it actually reads is “v is an ordinary pointer to an
    int that happens to be const.” That is, the const has bound itself to the int
    again, and the effect is the same as the previous definition. The fact that
    these two definitions are the same is the confusing point; to prevent this
    confusion on the part of your reader, you should probably stick to the first
    form.


    const pointer

    To make the pointer itself a const, you must place the const specifier to the
    right of the *, like this:

    int d = 1;
    int* const w = &d;

    Now it reads: “w is a pointer, which is const, that points to an int.” Because
    the pointer itself is now the const, the compiler requires that it be given an
    initial value that will be unchanged for the life of that pointer. It’s OK,
    however, to change what that value points to by saying


    *w = 2;
    You can also make a const pointer to a const object using either of two
    legal forms:

    Code:
    int d = 1;
    const int* const x = &d;  // (1)
    int const* const x2 = &d; // (2)
    Now neither the pointer nor the object can be changed.


    Some people argue that the second form is more consistent because the
    const is always placed to the right of what it modifies. You’ll have to decide
    which is clearer for your particular coding style.


    Here are the above lines in a compileable file:

    Code:
    const int* u;
    int const* v;
    int d = 1;
    int* const w = &d;
    const int* const x = &d;  // (1)
    int const* const x2 = &d; // (2)
    int main() {} ///

  5. #5
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: const pointers and pointer to const

    const applies to what is on its immediate left, if nothing on the left then on the right.
    This should do.
    I find that it is very simple and it works, for more read that link i posted.

    Although, i must point out the right way to analyze is to read from right to left.
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  6. #6
    Join Date
    Sep 2004
    Location
    A Planet Called Earth... :-)
    Posts
    835

    Re: const pointers and pointer to const

    ????
    C++ program ran... C++ program crashed... C++ programmer quit !!

    Regards

    Shaq

  7. #7
    Join Date
    Jul 2005
    Posts
    121

    Re: const pointers and pointer to const

    thanx man
    but that's what is written in "Thinking in C++" book and that's what i'm reading now and that part get me confused
    first there is no problem with me in the definitions like int *const p;
    you’re saying that u can
    point to anything (that is, it is not const), but the thing it points to cannot
    be changed.
    and that's from this faq
    [http]http://www.codeguru.com/forum/showthread.php?t=231042[/http]
    const char*
    declares a pointer to a constant character (or a constant character array). The pointer can be changed, but the character (or array) to which it points can not be changed.
    how!
    if i change the or edit the pointer that will affect the character array too didn't it!
    or you mean that :
    if i assign a pointer to something then now the pointer can't not be modefied and i can perform some operation on it like assigning it to another " not constant character array " for example or sending it as an argument without changing it
    then after ending with it i can assign it to another " char* " for example . is it this way?


    const pointer:
    Because
    the pointer itself is now the const, the compiler requires that it be given an
    initial value that will be unchanged for the life of that pointer. It’s OK,
    however, to change what that value points to by saying


    *w = 2;
    and that's from this faq
    [http]http://www.codeguru.com/forum/showthread.php?t=231042[/http]
    char* const
    declares a constant pointer which has both read and write access to a character (or character array). The pointer itself is a constant and you can not change it.
    that's what really confused me
    they say that it's value can't be changed when they are assigning that value but then he says you can change what the value is pointing to or it's have a read and write access then saying the pointer can't changed

    what is your comments
    Last edited by cgtalk; September 22nd, 2005 at 03:38 AM. Reason: quoto to quote

  8. #8
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Lightbulb Re: const pointers and pointer to const

    have u check this Thinking C++ CareFully

    int *const p;
    &
    const char* p;
    i think u r looking for these two lines
    Code:
    To declare the object pointed to by the pointer as const 
    const    char *cpch;
    
    To declare the value of the pointer — that is, the actual address stored in 
    the pointer — as const 
    
    char * const    pchc;
    example
    Code:
    const char cch = 'A';
    char        ch = 'B';
    
    const char        *pch1 = &cch;
    const char *const  pch4 = &cch;
    const char        *pch5 = &ch;
    char              *pch6 = &ch;
    char       *const  pch7 = &ch;
    const char *const  pch8 = &ch;
    
    //The following declaration/initializations are erroneous.
    
    char *pch2 = &cch;        // Error
    char *const pch3 = &cch;  // Error
    The declaration of pch2 declares a pointer through which a constant object might be modified and is therefore disallowed. The declaration of pch3 specifies that the pointer is constant, not the object;

    another One
    Code:
    int *const cpObject = 0;
    int *pObject;
    
    void main()
    {
        pObject = cpObject; // OK
        cpObject = pObject; // Error
    }
    Last edited by humptydumpty; September 22nd, 2005 at 04:10 AM.

  9. #9
    Join Date
    Jul 2005
    Posts
    121

    Re: const pointers and pointer to const

    now every thing is clear
    i've understand it
    thank you guys

    but i have a another problem arise when i read the "stack" example

    how can i store:
    Code:
    string s[]={"hello","code"}
    or
    string* sss;
    or
    const string* ss;
    into
    Code:
    string * const p[2];  // 2 is optional
    i've tried hard but the error " C2440" arrives each time

  10. #10
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: const pointers and pointer to const

    Take a look at the following FAQ...

  11. #11
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    Re: const pointers and pointer to const

    Code:
    int a=12;
    const int b=2;
    int c=4;
    const int *p;
    int *const pp=&a;
    which is wrong and which is legal
    Code:
    p=&b;
    *p=33;
    pp=&c; // if this is legal then why the pointer is const if it's value changed
    The first one: p = &b; is legal because p points to const int, and b is a const int, so p can ponit to it.

    The other two are both illegal.

    Code:
    int *const cpObject = 0;
    int *pObject;
    
    void main()
    {
        pObject = cpObject; // OK
        cpObject = pObject; // Error
    }
    also an error that your main returns void instead of int.
    Last edited by NMTop40; September 22nd, 2005 at 09:49 AM.

  12. #12
    Join Date
    Jul 2005
    Posts
    121

    Re: const pointers and pointer to const

    no problem with const now but i need an answer to my last question

    Code:
    string s[]={"hello","code"}
    string* const ps[5]={0};
    
    // any way to store s in ps
    const string* S="hello";
    const string *ss=&SomeString;
    //any way to store S or ss in the ps
    always 1 compile time error arrives "C2440"

  13. #13
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Thumbs up Re: const pointers and pointer to const

    Quote Originally Posted by cgtalk
    no problem with const now but i need an answer to my last question

    Code:
    string s[]={"hello","code"}
    string* const ps[5]={0};
    
    // any way to store s in ps
    const string* S="hello";
    const string *ss=&SomeString;
    //any way to store S or ss in the ps
    always 1 compile time error arrives "C2440"
    still Problem with const only.
    have you read all previous article carefully and Andreas Masur link.
    Because after that there is no problem to do this.
    your error occur due to Following.
    conversion' : cannot convert from 'type1' to 'type2'

    const string*
    declares a pointer to a constant string . The pointer can be changed, but the string to which it points can not be changed

    and you are trying to do same thing only.
    Better Once again read all the post and link and if still problem then ask in the forum

  14. #14
    Join Date
    Jul 2005
    Posts
    121

    Re: const pointers and pointer to const

    ok
    when i declare ps as a constant pointer to array of strings this should be initialized first so i initialize it to zeros
    so the values for each pointer could be changed couse it's a constant pointer
    then why when i try to assign a string to it i get a compile time error ,

    also for a pointer to constant string the string is initialized to something and i can't change the value inside the string which i didn't , i have try to aasign it to any index of the ps array nut the same compile time error happends

    did i understood the constants wrong , it isn't that way to deal with constants array of strings?
    i just want to strore a pointer to a constant string which i initialize it first
    like
    Code:
    const string* s="hello";
    the string will not be edited after this couse it's constant
    but i just want to store it like this way
    Code:
    string* const ps[3]={0}; // should be initialized first couse it's const pointer
    &ps[0]=*s;
    &ps[1]=*s;
    ..
    or does the error becouse the compiler think that after i store it in the array the value in that address could be modified becouse it's address of a constant pointer but it can't do that couse the string is a constant string?
    is this is the couse of the error?

  15. #15
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: const pointers and pointer to const

    Quote Originally Posted by cgtalk
    when i declare ps as a constant pointer to array of strings this should be initialized first so i initialize it to zeros
    A constant pointer to an array of strings must be initialized to points to an array of strings, like that for example:
    Code:
    string * const p=new string[42];
    
    delete[] p;
    Or like that:
    Code:
    string array[42];
    string * const p=array;
    But if you initialize the pointer to zero (=NULL), then it will points forever to NULL.
    Quote Originally Posted by cgtalk
    so the values for each pointer could be changed couse it's a constant pointer
    You mean "cause it's a pointer to non-constant data".
    In that case, you can change the strings, like that:
    Code:
    string array[42];
    string * const p=array;
    p[0]="TheFirstStringOfTheArray";
    Quote Originally Posted by cgtalk
    then why when i try to assign a string to it i get a compile time error ,
    How do you do that?

    Quote Originally Posted by cgtalk
    also for a pointer to constant string the string is initialized to something and i can't change the value inside the string which i didn't , i have try to aasign it to any index of the ps array nut the same compile time error happends
    The data pointed by a pointer to a constant string is not automatically created. Instead, you must create a new datum, and assign it to the pointer, like that:
    Code:
    const string* s; // pointer to a constant string - not initialized.
    string datum="hello!";
    s=&datum; // now s points to a string containing "hello!"
    // you cannot modify (without const_cast) the datum via s, but you can still change the datum via the datum variable.
    
    // you can make s points to another datum, like that:
    const string SecondDatum="another string!";
    s=&SecondDatum;
    Quote Originally Posted by cgtalk
    i just want to strore a pointer to a constant string which i initialize it first
    Code:
    const string* s="hello";
    This code is totally invalid!
    You must initialize (or assign) a pointer with an address, not with a value!
    Instead, you must do something like that:
    Code:
    const string a_const_string="hello";
    const string *ps=&a_const_string;
    Or, the same thing with an array:
    Code:
    const string an_array_of_const_strings[2]={"FirstString","SecondString"};
    const string *ps=an_array_of_const_strings;
    std::cout<<ps[1]; // outputs "SecondString"!
    
    ps[1]="another string"; // compile-time error - you cannot assign a const value!
    Note that you cannot call delete[] with a pointer to const data, since it 'touches' to the pointed data!
    Quote Originally Posted by cgtalk
    Code:
    string* const ps[3]={0}; // should be initialized first couse it's const pointer
    &ps[0]=*s;
    &ps[1]=*s;
    What is that code?
    ps is array of const pointers to strings (non-const strings).
    That is why the array must be initialized, but you initialize all const pointers to zero=NULL, so you will never be able to use this array.

    &ps[0]=*s; : It is an invalid assignment of a non-L-value (address)!
    What do you want to do?

    If you want to creates an array of const pointers to strings, you can do it like that:
    Code:
    string String1="hello",String2=" ",String3="world!";
    
    string * const ps[3]={&String1,&String2,&String3};
    
    for(size_t i=0;i<3;++i)
    	{
    	(*ps[i])+="[end-of-string]"; // modify String1, String2 or String3
    	}
    
    std::cout<<String1<<String2<<String3<<std::endl;
    // outputs "hello[end-of-string] [end-of-string]world![end-of-string]"
    But maybe you just want an array of const strings, like that:
    Code:
    const string array[3]={"string1","string2","string3"};

    Quote Originally Posted by cgtalk
    did i understood the constants wrong , it isn't that way to deal with constants array of strings?gtalk]
    You don't seem to understand correctly pointers.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured