Like Tree1Likes
  • 1 Post By iMalc

Different between int * const ptr ,const char *sPtr

This is a discussion on Different between int * const ptr ,const char *sPtr within the C Programming forums, part of the General Programming Boards category; I read something about Using the const qualifier with pointers on C How to Program (Fifth edition) [Page 269 if ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    102

    Different between int * const ptr ,const char *sPtr

    I read something about Using the const qualifier with pointers on C How to Program (Fifth edition) [Page 269 if you have it]. I know what a CONST is but I was wondering what is an

    • int* const ptr
    • const int *sPtr


    What's the different between this 2 things. Why the const are able to change position? Thanks

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    For the former, the const applies to the token of the left, i.e., the pointer itself is const. For the latter, it applies to the int, i.e., what the pointer points to is const.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    errr. i still lost

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    Q: What's the difference between const char *p, char const *p, and char * const p?
    A: The first two are interchangeable; they declare a pointer to a constant character (you can't change any pointed-to characters). char * const p declares a constant pointer to a (variable) character (i.e. you can't change the pointer).

    Srry i still don't understand. What's the purpose of declaring those things const char* p, char const *p, and char* const p. It make me blur! when a const is added.

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    Isn't const char*p means it's a pointer of character type with a condition not to modify it's value

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    Quote Originally Posted by ncode
    What's the purpose of declaring those things const char* p, char const *p, and char* const p.
    The first two are equivalent, and a common use for them is when you want to point to the first character of a string literal, e.g.,
    Code:
    const char *s = "hello";
    This way, if you attempt to modify the string literal through the pointer, the compiler will complain, which is a Good Thing since modifying a string literal results in undefined behaviour.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Code:
    const char* p
    You can modify this pointer to point to something else, but whatever thing it points to at any given moment, you can't modify that thing using this pointer. char const *p means the same thing.


    Code:
    char* const p
    You can't modify this pointer to point to something else at all, but you can modify the thing that it forever points to as much as you like.


    Last, but not least, you can combine these two:
    Code:
    const char* const p
    This pointer can't be made to point to something else, and the data that it points to also can't be modified.
    rags_to_riches likes this.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    102
    aw. I'm still in the forest finding my path. It's so hard to understand. Maybe i would ignore this first

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-20-2011, 02:19 PM
  2. Replies: 3
    Last Post: 11-15-2009, 04:57 AM
  3. Replies: 1
    Last Post: 04-03-2009, 09:52 AM
  4. Replies: 7
    Last Post: 04-28-2008, 10:20 AM
  5. const wchar_t * to UTF-8 const char* p
    By Hulag in forum C++ Programming
    Replies: 12
    Last Post: 11-25-2006, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21