Difference between const char * and char const *

This is a discussion on Difference between const char * and char const * within the C Programming forums, part of the General Programming Boards category; Hi, Can any one please explain me the difference between using a const char * or a char const *. ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    1

    Question Difference between const char * and char const *

    Hi,

    Can any one please explain me the difference between using a const char * or a char const *. I used both in the below program and everything worked fine except for a warning when I compiled.

    Code:
    int main()
    {
       const char *s;   //Or char const *s;
       s = (char *) malloc (10);
       strcpy(s, "abc");
       return 0;
    }
    Thanks in advance!!!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    const char * and char const * both mean the same thing -- a pointer to chars that you cannot change (you can change the pointer, so the malloc is ok, but you CANNOT change the characters pointed to, so the strcpy is bad).

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,205
    The basic rule of thumb with const (and volatile) keyword is that it applies to whatever is immediately to its left. If there is nothing to its left, it applies to whatever is immediately to its right. The standard is a bit more specific in how it describes this, but comes essentially to the same thing.

    By this logic, "const char *" is a (non-const) pointer to a const char, and "char const *" means the same thing.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,628
    FYI, the warning was probably due to casting malloc's return value.

    void * malloc (size_t nbytes);

    There is no reason to cast the return value of malloc in C anymore because void pointers are assignable to pointers of any type.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,205
    Actually, the warning would be on the strcpy() call. The first argument of strcpy() is copied to, so is not a pointer to const. But you are passing a const char * as first argument .....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unkown Hang
    By Bladactania in forum C Programming
    Replies: 31
    Last Post: 04-22-2009, 09:33 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  4. Try out my new game :) !
    By Stan100 in forum Game Programming
    Replies: 10
    Last Post: 06-05-2003, 08:10 AM
  5. AnsiString versus char *
    By Zahl in forum C++ Programming
    Replies: 35
    Last Post: 10-16-2002, 08:38 PM

Tags for this Thread


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