Thread: Issue with pointer to a const char using typedef

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    54

    Issue with pointer to a const char using typedef

    I have declared a pointer to const char and incremented the pointer, it is not giving any issue.
    Code:
    int main()
    {
            const char * ptr;
            char *c = "ABC";
            ptr = c;
            ptr++;
    
    
            return 0;
    }
    I have used typedef for declaring a pointer to const char as shown below, but it is giving error during compilation. It seems it is treating pointer as constant pointer.

    Code:
    typedef char *P;
    int main()
    {
    
    
            const P ptr;
            char *c = "ABC";
            ptr = c;
            ptr++;
    
    
            return 0;
    }
    Error during compilation:
    Code:
    test.c: In function ‘main’:
    test.c:10:2: error: assignment of read-only variable ‘ptr’
    test.c:11:2: error: increment of read-only variable ‘ptr’
    I think typedef is used for defining typedefs, in this case ptr is treated as constant pointer, not a pointer to constant char.
    Code:
    typedef char *P;
    const P ptr;
    Is there any limitations with typedef in C.

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    What would you expect would be constant in `typedef int p` and `const P ptr` statements?

    The `ptr` variable is what remains a constant.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    First of all, 'c' is assigned the address of a CONSTANT string, "ABC", which cannot be altered, so for the examples below, I will use a different string example:
    Code:
    char str[] = "ABC";
    This defines a non-const char array, initialized with a CONSTANT string, "ABC". You now have two copies of the string, one non-const (the array), and the other const.

    Code:
    typedef char * P;
    
    const P ptr = str;
    This declares a const pointer to a non-const char. The const pointer would need to be initialized!

    Code:
    typedef char const * P;
    P ptr = str;
    In this case, it declares a non-const pointer to a const char.

    And to be thorough:
    Code:
    typedef char const * P;
    const P ptr = str;
    ptr is a const pointer to a const char. neither can be altered.

    I hope this has help answered your question, rather than confusing you even more! ;^)

    There are many tutorials on interpreting complex pointer definitions, this is but one of them.

  4. #4
    Registered User
    Join Date
    Feb 2014
    Posts
    54
    Thanks for the info.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Char Pointer Issue
    By Layvian in forum C Programming
    Replies: 8
    Last Post: 05-18-2012, 02:07 PM
  2. Replies: 4
    Last Post: 04-20-2011, 01:19 PM
  3. Replies: 1
    Last Post: 03-27-2010, 06:50 AM
  4. Replies: 3
    Last Post: 11-15-2009, 04:57 AM
  5. typedef for pointer to array of char
    By curlious in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2003, 04:21 PM

Tags for this Thread