Why string literals are regarded as char * not as const char *?
(1) void f(char *);
(2) void f(const char *);
f("foo") will call version (1) of function f.
I understand that the exact type of literal "foo" is char[4], which by means
of standard conversion becomes char *. Still, there's something going wrong
here, because this allows modification of "foo", which in my opinion should
be forbidden (because it causes undefined behavior).
Best regards,
Marcin 10 3634
Marcin Kalicinski wrote: Why string literals are regarded as char * not as const char *?
(1) void f(char *); (2) void f(const char *);
f("foo") will call version (1) of function f.
I understand that the exact type of literal "foo" is char[4], which by means of standard conversion becomes char *. Still, there's something going wrong here, because this allows modification of "foo", which in my opinion should be forbidden (because it causes undefined behavior).
Best regards, Marcin
It should be const. Section 2.13.4.1 states
``An ordinary string literal has type "array of n const char" and a
static storage duration where n is the size of the string as
defined below, and is initialized with the given characters.''
HTH,
Jacques
Hi,
So it seems there's something wrong with VC++ .NET compiler. The following
code:
//=============== ===========
#include <iostream>
void main()
{
std::cout << typeid("foo").n ame() << "\n";
}
//=============== ===========
Outputs "char[4]". And the following one:
//=============== ===========
#include <iostream>
int f(char *) { return 1; }
int f(const char *) { return 2; }
void main()
{
std::cout << f("foo") << "\n";
}
//=============== ===========
Outputs 1.
At the moment I do not have access to any other compiler, but I wonder if
this is normal behavior? If the standard says it should be const char [4],
it's a serious problem if this is char[4] instead.
Best regards,
Marcin Marcin Kalicinski wrote: Why string literals are regarded as char * not as const char *?
(1) void f(char *); (2) void f(const char *);
f("foo") will call version (1) of function f.
I understand that the exact type of literal "foo" is char[4], which by
means of standard conversion becomes char *. Still, there's something going
wrong here, because this allows modification of "foo", which in my opinion
should be forbidden (because it causes undefined behavior).
Best regards, Marcin
It should be const. Section 2.13.4.1 states ``An ordinary string literal has type "array of n const char" and a static storage duration where n is the size of the string as defined below, and is initialized with the given characters.''
HTH, Jacques
Marcin Kalicinski wrote: Hi,
So it seems there's something wrong with VC++ .NET compiler. The following code:
//=============== =========== #include <iostream> void main() { std::cout << typeid("foo").n ame() << "\n"; } //=============== ===========
Outputs "char[4]". And the following one:
Interestingly enough, GCC 3.2.3 doesn't show the difference between
char[] and const char[] through typeid.name() -
cout << typeid(char[4]).name() << '\n'
<< typeid(const char[4]).name();
says
A4_c
A4_c
It calls the correct function though.. it matches void f(const char*).
HTH,
Jacques.
"Marcin Kalicinski" <ka****@poczta. onet.pl> wrote in message
news:bt******** **@korweta.task .gda.pl... //=============== =========== #include <iostream> int f(char *) { return 1; } int f(const char *) { return 2; } void main() { std::cout << f("foo") << "\n"; } //=============== ===========
Outputs 1.
On changing void main() to int main() and compiling using Comeau C++, the
output is 2. On mingw, the output is 2.
Curiously, the output is 1 on Borland 5.5.1.
Regards,
Sumit.
"Marcin Kalicinski" <ka****@poczta. onet.pl> skrev i melding
news:bt******** **@korweta.task .gda.pl... Hi,
So it seems there's something wrong with VC++ .NET compiler. The following code:
//=============== =========== #include <iostream> void main() { std::cout << typeid("foo").n ame() << "\n"; } //=============== ===========
Outputs "char[4]". And the following one:
//=============== =========== #include <iostream> int f(char *) { return 1; } int f(const char *) { return 2; } void main() { std::cout << f("foo") << "\n"; } //=============== ===========
This outputs 2 on Visual Studio .NET 2003 or VC++ 7.1
- Magnus
Hi,
I tested it on VC .NET (not 2003), compiler version 13.00.9466. Seems that
Microsoft are fixing their bugs :-)
Best regards,
Marcin
Użytkownik "Magnus" <no@spam.com> napisał w wiadomości
news:3f******** @news.broadpark .no... "Marcin Kalicinski" <ka****@poczta. onet.pl> skrev i melding news:bt******** **@korweta.task .gda.pl... Hi,
So it seems there's something wrong with VC++ .NET compiler. The
following code:
//=============== =========== #include <iostream> void main() { std::cout << typeid("foo").n ame() << "\n"; } //=============== ===========
Outputs "char[4]". And the following one:
//=============== =========== #include <iostream> int f(char *) { return 1; } int f(const char *) { return 2; } void main() { std::cout << f("foo") << "\n"; } //=============== ===========
This outputs 2 on Visual Studio .NET 2003 or VC++ 7.1
- Magnus
"Marcin Kalicinski" <ka****@poczta. onet.pl> wrote in message news:bt******** **@korweta.task .gda.pl... Why string literals are regarded as char * not as const char *?
(1) void f(char *); (2) void f(const char *);
f("foo") will call version (1) of function f.
Your compiler is broken. There are two "exact match"
conversion sequences from string literal to char* and const char*.
The overloads above are ambiguous.
There is a deprecated conversion from string literal to char* which exists to
codify 25 years of sloppy C programming.
"Jacques Labuschagne" <ja*****@clawsh rimp.com> wrote in message
news:qt******** ************@ne ws02.tsnz.net.. . Marcin Kalicinski wrote: Why string literals are regarded as char * not as const char *?
(1) void f(char *); (2) void f(const char *);
f("foo") will call version (1) of function f.
I understand that the exact type of literal "foo" is char[4], which by
means of standard conversion becomes char *. Still, there's something going
wrong here, because this allows modification of "foo", which in my opinion
should be forbidden (because it causes undefined behavior).
The conversion to char * is allowed for compatibility with lots of old code
that uses char * as though it were const char *.
Best regards, Marcin
It should be const. Section 2.13.4.1 states ``An ordinary string literal has type "array of n const char" and a static storage duration where n is the size of the string as defined below, and is initialized with the given characters.''
There's also 4.2.2:
"A string literal (2.13.4) that is not a wide string literal can be
converted to an rvalue of type "pointer to
char"; a wide string literal can be converted to an rvalue of type "pointer
to wchar_t". In either case,
the result is a pointer to the first element of the array. This conversion
is considered only when there is an
explicit appropriate pointer target type, and not when there is a general
need to convert from an lvalue to an rvalue. [Note: this conversion is
deprecated. See Annex D. ] For the purpose of ranking in overload resolution
(13.3.3.1.1), this conversion is considered an array to pointer conversion
followed by a qualification conversion (4.4). [Example: "abc" is converted
to "pointer to const char" as an array to pointer conversion, and then to
"pointer to char" as a qualification conversion. ]
DW
"Marcin Kalicinski" <ka****@poczta. onet.pl> wrote in message news:<bt******* ***@korweta.tas k.gda.pl>... Hi,
So it seems there's something wrong with VC++ .NET compiler. The following code:
M$ products, BAH!!!!
My GCC 3.3.2 will work fine with that and call the const char * version //=============== =========== #include <iostream> void main() { std::cout << typeid("foo").n ame() << "\n"; } //=============== ===========
Outputs "char[4]". And the following one:
//=============== =========== #include <iostream> int f(char *) { return 1; } int f(const char *) { return 2; } void main() { std::cout << f("foo") << "\n"; } //=============== ===========
Outputs 1.
At the moment I do not have access to any other compiler, but I wonder if this is normal behavior? If the standard says it should be const char [4], it's a serious problem if this is char[4] instead.
Best regards, Marcin
Marcin Kalicinski wrote: Why string literals are regarded as char * not as const char *?
(1) void f(char *); (2) void f(const char *);
f("foo") will call version (1) of function f.
I understand that the exact type of literal "foo" is char[4], which by means of standard conversion becomes char *. Still, there's something going wrong here, because this allows modification of "foo", which in my opinion should be forbidden (because it causes undefined behavior).
Best regards, Marcin
It should be const. Section 2.13.4.1 states ``An ordinary string literal has type "array of n const char" and a static storage duration where n is the size of the string as defined below, and is initialized with the given characters.''
HTH, Jacques This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Don Starr |
last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string
(including nul), or the size of a pointer?
For example, assuming a char is 1 byte and a char * is 4 bytes, should the following yield 4, 5, of
something else? (And, if something else, what determines the result?)
char x = "abcd";
printf( "%d\n", sizeof( x ) );
-Don
|
by: Olivier Bellemare |
last post by:
I've tried to make a function that returns the middle of a string. For
example:
strmid("this is a text",6,4); would return "is a".
Here is my code:
char *strmid(char *texte, int depart, int longueur)
{ char *resultat = " ";
char *temporaire = " ";
int nbr;
|
by: al |
last post by:
char s = "This string literal";
or
char *s= "This string literal";
Both define a string literal. Both suppose to be read-only and not to be
modified according to Standard. And both have type of "const char *".
Right? But why does the compiler I am using allow s to be modified, instead
of generating compile error?
|
by: Janice |
last post by:
char* line = "abcd";
How to convert the line to upper case and print?
Any option for printf to do this?
Thanx
|
by: kobu.selva |
last post by:
I was recently part of a little debate on the issue of whether
constants and string literals are considered "data objects" in C. I'm
more confused now than before.
I was always under the understanding that only "named" storage
areas(from the standard) were data objects. Since constants and string
literals don't have lvalues, they can't be data objects. Yet, I was
shown the first page of chapter 2 in K&R2, which states that variables...
| |
by: junky_fellow |
last post by:
what would be the output for the following piece of code ?
if ( "hello" == "hello" )
printf("True\n");
else
printf("False\n");
What is the reason for that ?
|
by: Dead Loop |
last post by:
Hi all,
I'm a beginner and my question is:
Are there any differences between
char *p = "Hello, world!";
and
const char *p = "Hello, world!";
?
|
by: arnuld |
last post by:
i tried to output these 2 to the std. output:
const std::string hello = "Hello";
const std::string message = hello + ", world" + "!";
const std::string exclam = "!";
const std::string message2 = "hello" + " world" + exclam;
the first one runs fine but 2nd does not as we can not combine 2
|
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 example will produce an error as we are trying to
modify a constant.
So, str1 is not a string constant in C++.
|
by: polas |
last post by:
Good morning,
I have a quick question to clear up some confusion in my mind. I
understand that using a string literal in a declaration such as char
*p = "string literal" declares a pointer to memory holding the string
and the string might very well be held in read only memory.
However, I am sure that I read somewhere that the declaration char a
= "string literal", even though a is an array (and I understand the
differences between...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |