#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    73
    Rep Power
    12

    const char* Vs char*


    hi
    whats the difference between const char* and char*? i need to append data (data varies in size) to char array and then write it to a text file. which one of these serves the purpose.

    thanks in advance
  2. #2
  3. not a fan of fascism (n00b)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Feb 2003
    Location
    ct
    Posts
    2,756
    Rep Power
    95
    a const char * is a pointer that always points to the same location i believe. a char * is just a regular pointer to a character. you should use a regular char * for your needs
  4. #3
  5. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,111
    Rep Power
    1803
    Declaring a parameter const char*prevents the function being called from moving the pointer. The data pointed to however may be changed. In C++ a reference parameter
    char& does a similar job and may be clearer. A char* will work, but the protection is not there, and does not indicate intent to the user of the function.

    Clifford
    Last edited by clifford; August 23rd, 2003 at 03:15 AM.
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    Originally posted by clifford
    Declaring a parameter const char*prevents the function being called from moving the pointer. The data pointed to however may be changed. In C++ a reference parameter
    char& does a similar job and may be clearer. A char* will work, but the protection is not there, and does not indicate intent to the user of the function.
    You're confusing const char * and char * const declarations. You can move the const char * ptr, but you can't assign to the data being pointed to (if you couldn't move the pointer, strcpy(), strcmp() etc. would never work). See What's the meaning of: int const *pt for a slightly long winded explanation about different declarations using const.
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,111
    Rep Power
    1803
    I was going to argue that point, but had the sense to check, and Scorpions4ever is entirely correct.

    From Stroustrop:


    Code:
    char *const cp    // const pointer to char
    char const* pc    // pointer to const char
    const char* pc    // pointer to const char
    Clifford

IMN logo majestic logo threadwatch logo seochat tools logo