473,385 Members | 1,593 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,385 software developers and data experts.

assigning string constant to char *

The first of the following functions compiles, the second gives what
I think is a spurious error:

"cannot convert `const char[5]' to `char *' in assignment".

void foo(int m) {
char *str;
if (m < 4) str = "%01x";
else if (m < 9) str = "%02x";
else str = "%03x";
}

void foo(int m) {
char *str;
str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
}

Any idea why gcc doesn't like the second version? If I compile
it as a C file instead of C++, it works fine.

Steve
Aug 25 '06 #1
10 9433
Steve Pope wrote:
The first of the following functions compiles, the second gives what
I think is a spurious error:

"cannot convert `const char[5]' to `char *' in assignment".

void foo(int m) {
char *str;
if (m < 4) str = "%01x";
else if (m < 9) str = "%02x";
else str = "%03x";
}

void foo(int m) {
char *str;
str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
}

Any idea why gcc doesn't like the second version? If I compile
it as a C file instead of C++, it works fine.
It is not a spurious error, just an obscure one. A string constant
(like "%01x") is actually of type const char[5], which decays to const
char*. For historical reasons (so as not to break a lot of existing
code), you can assign a string constant to a non-const char*, although
it bends (if not breaks) const-correctness. In the first example, you
are assigning a string constant to a char*, and that's permitted. In
the second example, you are assigning not a string constant, but an
expression instead, to the char*. That's not permitted - the exception
to const-correctness is intentionally kept narrow.

To solve your problem change your second example to the following:

void foo(int m) {
const char *str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
}

Since you're now assigning to a const char*, not to a non-const char*,
the problem disappears.

Best regards,

Tom

Aug 25 '06 #2
Thomas Tutone <Th***********@yahoo.comwrote:
>Steve Pope wrote:
>void foo(int m) {
char *str;
str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
}
>Any idea why gcc doesn't like the second version? If I compile
it as a C file instead of C++, it works fine.
>It is not a spurious error, just an obscure one. A string constant
(like "%01x") is actually of type const char[5], which decays to const
char*. For historical reasons (so as not to break a lot of existing
code), you can assign a string constant to a non-const char*, although
it bends (if not breaks) const-correctness. In the first example, you
are assigning a string constant to a char*, and that's permitted. In
the second example, you are assigning not a string constant, but an
expression instead, to the char*. That's not permitted - the exception
to const-correctness is intentionally kept narrow.
Okay, that makes sense.
>To solve your problem change your second example to the following:
>void foo(int m) {
const char *str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
}
>Since you're now assigning to a const char*, not to a non-const char*,
the problem disappears.
That works, but oddly so does the following:

const char *str;
str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
if (m 12) str = "%04x";

It seems the "const" does not mean as much for type char * as
it does for other types.

Steve
>
Best regards,

Tom

Aug 25 '06 #3
Steve Pope wrote:
It seems the "const" does not mean as much for type char * as
it does for other types.
It's an issue related not to the char type, but to the string literals:
the following conversion are allowed:
string literal -const char*
string literal -char* // deprecated, but valid

but, of course, it's not valid
const char* -char*

so, you can convert string literal to char* only directly, not through
const char* (as you tried with the conditional operator '?').

Bye,

Zeppe
Aug 25 '06 #4

Steve Pope skrev:

[snip]
>
Okay, that makes sense.
To solve your problem change your second example to the following:
void foo(int m) {
const char *str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
}
Since you're now assigning to a const char*, not to a non-const char*,
the problem disappears.

That works, but oddly so does the following:

const char *str;
str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
if (m 12) str = "%04x";

It seems the "const" does not mean as much for type char * as
it does for other types.
Well no.... as Thomas Tutone explained, you can break constness for
char only. But what surprises you above is that you can reassign str.
This is because you read the type wrongly. const char *str means that
str is a pointer to a constant char. The pointer is by itself not
constant - you can assign it to anything you want (and actually you
should be surprised by both assignments above). If you wanted the
pointer to be constant, the notation would have to be char * const str
(and then you better remember to initialise it!). This way the content
of *str would be changeable.

/Peter
>
Steve

Best regards,

Tom
Aug 25 '06 #5

Steve Pope wrote:
Thomas Tutone <Th***********@yahoo.comwrote:
[snip]
It is not a spurious error, just an obscure one. A string constant
(like "%01x") is actually of type const char[5], which decays to const
char*. For historical reasons (so as not to break a lot of existing
code), you can assign a string constant to a non-const char*, although
it bends (if not breaks) const-correctness. In the first example, you
are assigning a string constant to a char*, and that's permitted. In
the second example, you are assigning not a string constant, but an
expression instead, to the char*. That's not permitted - the exception
to const-correctness is intentionally kept narrow.

Okay, that makes sense.
To solve your problem change your second example to the following:
void foo(int m) {
const char *str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
}
Since you're now assigning to a const char*, not to a non-const char*,
the problem disappears.

That works, but oddly so does the following:

const char *str;
str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
if (m 12) str = "%04x";

It seems the "const" does not mean as much for type char * as
it does for other types.
Sorry, my changing your example so that the str was initialized upon
creation confused matters. A const char* means "a pointer to a char
that is const," not "a const pointer to a char." The latter would be
char* const. You can reassign a const char*, you just can't change the
contents of what it points to.

Best regards,

Tom

Aug 25 '06 #6
Steve Pope wrote:
Thomas Tutone <Th***********@yahoo.comwrote:
[..]
>To solve your problem change your second example to the following:
>void foo(int m) {
const char *str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
}
>Since you're now assigning to a const char*, not to a non-const
char*, the problem disappears.

That works, but oddly so does the following:

const char *str;
str = m < 4 ? "%01x" : m < 9 ? "%02x" : "%03x";
if (m 12) str = "%04x";

It seems the "const" does not mean as much for type char * as
it does for other types.
You confuse your consts. In

const char * str;

'const' relates to the char, not to the char*. You need to read up on
declaration syntax and its meaning.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 25 '06 #7
zeppe <ze***@nospam.remove.all.this.email.itwrote:
>Steve Pope wrote:
>It seems the "const" does not mean as much for type char * as
it does for other types.
It's an issue related not to the char type, but to the string
literals: the following conversion are allowed:
string literal -const char*
string literal -char* // deprecated, but valid

but, of course, it's not valid
const char* -char*

so, you can convert string literal to char* only directly, not through
const char* (as you tried with the conditional operator '?').
Seems that *is* related to the type char *, since the following
code also compiles, whereas analogous code with str of any type
other than char * does not compile. To me it looks like gcc
at least has special-cased "const char *" to not actually be const.

char a[10], b[10];

void foo(int m) {
const char *str;
str = a;
if (m 12) str = b;
}

Cheers,
Steve
Aug 25 '06 #8
Steve Pope wrote:
[..] To me it looks like gcc
at least has special-cased "const char *" to not actually be const.

char a[10], b[10];

void foo(int m) {
const char *str;
str = a;
if (m 12) str = b;
}
There you go again... 'str' is a non-const pointer to const char.
You can assign new value to it from an array (which when used in
an exrpession like assignment above decays to a pointer to char).

If you want 'str' to be non-changeable pointer, you need to declare
*it itself* const:

char * const str;

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Aug 25 '06 #9
Steve Pope wrote:
>but, of course, it's not valid
const char* -char*
Seems that *is* related to the type char *, since the following
code also compiles, whereas analogous code with str of any type
other than char * does not compile. To me it looks like gcc
at least has special-cased "const char *" to not actually be const.

char a[10], b[10];

void foo(int m) {
const char *str;
str = a;
if (m 12) str = b;
}
Actually, this is the opposite cast, i.e.:
char* -const char*

that of course is correct (and the above code works also with int*,
double* and any other pointer).
BTW, it seems that we are talking about two different things: I was
explaining the strange behaviour due to the conversion from literal to
char*, and you are referring to the fact that is seems possible o change
value to a const variable (that is, as others explained, is not const,
but it's const the value the pointer is referring to).
Bye,

Zeppe
Aug 25 '06 #10
peter koch <pe***************@gmail.comwrote:
>But what surprises you above is that you can reassign str.
This is because you read the type wrongly. const char *str means that
str is a pointer to a constant char. The pointer is by itself not
constant - you can assign it to anything you want (and actually you
should be surprised by both assignments above). If you wanted the
pointer to be constant, the notation would have to be char * const str
(and then you better remember to initialise it!).
Thanks, that does explain my confusion here.

Steve
Aug 25 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Anton Pervukhin | last post by:
Hello! Imagine the situation you have a class that interprets the command line of the program you are executing. This class is written by third party and has a main function parse with arguments...
3
by: Senthilraja | last post by:
I am able to compile the following code and get "hello" as ouput everytime I execute it:- #include <stdio.h> int main (void) { char *str; str="hello"; puts(str); return 0;
4
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
1
by: Arun | last post by:
Hi all, The code below gives compilation error (in gcc). "initializer element is not constant" @line 11 1 #include <stdio.h> 2 char* ptr="string"; 3 //char ptr="string"; 4 5 typedef struct...
14
by: nospam | last post by:
From the book "There is an important difference between these definitions: char amessage="now is the time"; char *pmessage ="now is the time"; snip On the other hand, pmessage is a...
4
by: allan.mcrae | last post by:
As part of a very simple memory leak detector, I am trying to store the value of __FILE__ in a char*. Since gcc4.2 I get the following warning... warning: deprecated conversion from string...
20
by: karthikbalaguru | last post by:
Hi, String constant being modifiable in C++ but, not modifiable in C. that is, In C++, the following example output will be "Mplusplus" char *str1 = "Cplusplus"; *str1 = 'M'; In C, the above...
13
by: sinbad | last post by:
hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

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.