Thread: question about pointers

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    38

    question about pointers

    if i had

    int y, x;
    const int *ptr=&y;

    can i assign

    ptr = &x;
    Last edited by kuwait; 12-14-2002 at 04:07 PM.
    please set free our POWs

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Yes. The pointer points to a (const int). It's not the pointer that is constant.
    And you can have a const XXX pointer point at XXX (since something read and writable can be assumed only readable) but you cannot have a XXX pointer point at const XXX, since the pointer would allow you to write to something that is unwritable.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    38
    thanx for answering

    another question

    will x be constant in this case?
    please set free our POWs

  4. #4
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by kuwait
    thanx for answering

    another question

    will x be constant in this case?
    Sure, but it depends on which way you look at it. ptr promises not to change x, but if you had another pointer pointing to x (non-const) you could change it (or even just change x directly).

    for example:

    int x = 32;
    const int *cptr = &x;
    int *ptr = &x;

    (*ptr)++; // Legal
    (*cptr)++; // Illegal, cptr promises not to change the data it point to


    Edit: Also worth mentioning that you can specify const pointers; using your first example, we can make it illegal to change what ptr points to, as well as make it illegal to change the data it points to.

    int x,y;
    const int * const ptr = &x;

    ptr = &y; // Illegal, ptr is const
    Last edited by Eibro; 12-14-2002 at 05:47 PM.

  5. #5
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    the pointer is const not x, x can be changed but not by that pointer.
    none...

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    if you want the pointer itself to be constant, then put const after the asterisk, IE

    int* const ptr = &x;

    ptr = &y; // ERROR: ptr is a constant

    you can also do

    const int* const ptr = &x;

    which is a constant pointer to a constant int

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Nominate this for the FAQ
    Everyone give it a good score now...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM