August 22nd, 2003, 12:03 PM
-
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
August 22nd, 2003, 12:15 PM
-
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
August 22nd, 2003, 03:04 PM
-
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.
August 22nd, 2003, 03:41 PM
-
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
August 23rd, 2003, 03:14 AM
-
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