By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,175 Members | 1,710 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,175 IT Pros & Developers. It's quick & easy.

what dose "const char const* var" mean?

P: n/a
s88
Hi all:
I saw the code likes...

7 #include <stdio.h>
8 int main(void){
9 const char *const green = "\033[0;40;32m";
10 const char *const normal = "\033[0m";
11 printf("%sHello World%s\n", green, normal);
12 return 0;
13 }

what's the different between "const char *green" and "const char *const
green"???
Thank you!

Nov 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
s88 wrote:
Hi all:
I saw the code likes...

7 #include <stdio.h>
8 int main(void){
9 const char *const green = "\033[0;40;32m";
10 const char *const normal = "\033[0m";
11 printf("%sHello World%s\n", green, normal);
12 return 0;
13 }

what's the different between "const char *green" and "const char *const
green"???


const char *green (or, equivalently, char const *green) points to
char and you cannot modify the pointed to object using green,
i.e.
const char *green = "verde";
green = "gruen";
is okay, but
green[0] = 'V';
strcpy(green, "Gruen");
are not allowed.

char *const green is a constant pointer to char, so you cannot modify
green but you can modify the object green points to.
char buf[42];
char *const green = buf;
strcpy(green, "verde");
green[0] = "V";
is okay, but
char *const green = buf;
green = "gruen";
is not allowed.

const char *const green = "gruen"; gives you a constant pointer to
const char, i.e. green will always point to the address where the
string "gruen" starts and cannot be used to change that string.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #2

P: n/a

Michael Mair wrote:
s88 wrote:
Hi all:
I saw the code likes...

7 #include <stdio.h>
8 int main(void){
9 const char *const green = "\033[0;40;32m";
10 const char *const normal = "\033[0m";
11 printf("%sHello World%s\n", green, normal);
12 return 0;
13 }

what's the different between "const char *green" and "const char *const green"???
const char *green (or, equivalently, char const *green) points to
char and you cannot modify the pointed to object using green,
i.e.
const char *green = "verde";
green = "gruen";
is okay, but
green[0] = 'V';
strcpy(green, "Gruen");
are not allowed.

char *const green is a constant pointer to char, so you cannot modify
green but you can modify the object green points to.
char buf[42];
char *const green = buf;
strcpy(green, "verde");
green[0] = "V";
is okay, but
char *const green = buf;
green = "gruen";
is not allowed.


Just to add:

const char *const green = "Verde";
In this you can't change any thing.

Regards
Ranjeet

const char *const green = "gruen"; gives you a constant pointer to
const char, i.e. green will always point to the address where the
string "gruen" starts and cannot be used to change that string.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Nov 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.