468,272 Members | 2,149 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

what dose "const char const* var" mean?

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
2 6212
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

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.

Similar topics

2 posts views Thread by Pinnacle | last post: by
4 posts views Thread by C. J. Clegg | last post: by
2 posts views Thread by ragged_hippy | last post: by
20 posts views Thread by liujiaping | last post: by
9 posts views Thread by Peithon | last post: by
2 posts views Thread by itsolution | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.