473,395 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 6461
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: G Patel | last post by:
I've been looking at some code for string functions (certain implementation of them) and the non modified string is usually declared as a const char *s in the parameter list. I was wondering,...
2
by: Pinnacle | last post by:
Can anyone explain what is "const char* const*" mean??
24
by: kevin.hall | last post by:
Is char** (or char*) implicitly convertible to 'const char * const *'? I couldn't find anything about it in the standard. MSVS 8.0 allows this. I'm curious if I'll run into trouble with other...
4
by: C. J. Clegg | last post by:
A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple...
2
by: ragged_hippy | last post by:
Hi, If I have a method that has string reference as a parameter, what happens if I pass a const char* variable to this method? One thought is that a temporary string will be created in the...
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
9
by: Peithon | last post by:
Hi, This is a very simple question but I couldn't find it in your FAQ. I'm using VC++ and compiling a C program, using the /TC flag. I've got a function for comparing two strings int...
2
by: itsolution | last post by:
Hi Guys, When a function has following arg type, update_data(const char *const * update_list,...) exactly what object type update_list can take? For example, I can call update_aaa() ...
7
by: Bill Davy | last post by:
I want to be able to write (const char*)v where v is an item of type Class::ToolTypeT where ToolTypeT is an enumeration and I've tried everything that looks sensible. There's an ugly solution, but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.