* const char * vs char[]
@ 2004-03-23 12:01 Massimiliano Cialdi
2004-03-23 12:19 ` Glynn Clements
2004-03-23 13:44 ` Mariano Moreyra
0 siblings, 2 replies; 8+ messages in thread
From: Massimiliano Cialdi @ 2004-03-23 12:01 UTC (permalink / raw)
To: linux-c-programming
I have this:
#include <stdio.h>
const char *str = "pippo";
int main(void)
{
void stampa(char s[]);
stampa(str);
return 0;
}
void stampa(char s[])
{
printf("%s\n", s);
}
and gcc gives me a warning:
pippo.c:9: warning: passing arg 1 of `stampa' discards qualifiers from
pointer target type
what are the differences between char[] and const char *?
thanks
--
Massimiliano Cialdi
cialdi@firenze.net
m.cialdi@oksys.it
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: const char * vs char[]
2004-03-23 12:01 const char * vs char[] Massimiliano Cialdi
@ 2004-03-23 12:19 ` Glynn Clements
2004-03-23 13:44 ` Mariano Moreyra
1 sibling, 0 replies; 8+ messages in thread
From: Glynn Clements @ 2004-03-23 12:19 UTC (permalink / raw)
To: Massimiliano Cialdi; +Cc: linux-c-programming
Massimiliano Cialdi wrote:
> #include <stdio.h>
> const char *str = "pippo";
> int main(void)
> {
> void stampa(char s[]);
> stampa(str);
> return 0;
> }
> void stampa(char s[])
> {
> printf("%s\n", s);
> }
>
> and gcc gives me a warning:
> pippo.c:9: warning: passing arg 1 of `stampa' discards qualifiers from
> pointer target type
>
>
> what are the differences between char[] and const char *?
1. char[] is an array while char* is a pointer. There is actually a
difference, although it doesn't matter here: arrays are always passed
by reference, so a declaration of the form:
void stampa(char s[])
is silently converted to:
void stampa(char *s)
by the compiler.
2. The variable is declared as "const char *", i.e. a pointer to
constant (unmodifiable) characters, but is being passed to a function
which expects a pointer to (modifiable) characters.
If stampa() doesn't modify the text, declare it as:
void stampa(const char *s)
If it does modify the text, then your program will segfault, because
string literals are normally stored in non-writable memory (regardless
of whether you include "const" in the variable's declaration).
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 8+ messages in thread
* RE: const char * vs char[]
2004-03-23 12:01 const char * vs char[] Massimiliano Cialdi
2004-03-23 12:19 ` Glynn Clements
@ 2004-03-23 13:44 ` Mariano Moreyra
2004-03-23 14:19 ` Massimiliano Cialdi
1 sibling, 1 reply; 8+ messages in thread
From: Mariano Moreyra @ 2004-03-23 13:44 UTC (permalink / raw)
To: 'Massimiliano Cialdi', 'linux-c-programming'
I think that the problem is not a difference between "char*" and "char[]"
The problem is that stampa is receiving a "char[]" and you are passing a
"const char*" as the arg.
The "qualifiers from pointer targer type" refers to the "const" part of the
"const char*" declaration I belive.
If you declare stampa like this:
void stampa(const char s[]);
you won't have this warning anymore.
-----Mensaje original-----
De: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org]En nombre de
Massimiliano Cialdi
Enviado el: Martes, 23 de Marzo de 2004 09:01
Para: linux-c-programming
Asunto: const char * vs char[]
I have this:
#include <stdio.h>
const char *str = "pippo";
int main(void)
{
void stampa(char s[]);
stampa(str);
return 0;
}
void stampa(char s[])
{
printf("%s\n", s);
}
and gcc gives me a warning:
pippo.c:9: warning: passing arg 1 of `stampa' discards qualifiers from
pointer target type
what are the differences between char[] and const char *?
thanks
--
Massimiliano Cialdi
cialdi@firenze.net
m.cialdi@oksys.it
-
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: const char * vs char[]
2004-03-23 13:44 ` Mariano Moreyra
@ 2004-03-23 14:19 ` Massimiliano Cialdi
2004-03-23 14:52 ` Mariano Moreyra
0 siblings, 1 reply; 8+ messages in thread
From: Massimiliano Cialdi @ 2004-03-23 14:19 UTC (permalink / raw)
To: linux-c-programming
On Tue, 23 Mar 2004 10:44:36 -0300
"Mariano Moreyra" <moremari@aca.org.ar> wrote:
> I think that the problem is not a difference between "char*" and
> "char[]" The problem is that stampa is receiving a "char[]" and you
> are passing a"const char*" as the arg.
> The "qualifiers from pointer targer type" refers to the "const" part
> of the"const char*" declaration I belive.
> If you declare stampa like this:
>
> void stampa(const char s[]);
>
> you won't have this warning anymore.
so what is a synonym of char[]? Maybe "char (const *)"?
thanks
--
Massimiliano Cialdi
cialdi@firenze.net
m.cialdi@oksys.it
^ permalink raw reply [flat|nested] 8+ messages in thread
* RE: const char * vs char[]
2004-03-23 14:19 ` Massimiliano Cialdi
@ 2004-03-23 14:52 ` Mariano Moreyra
2004-03-23 17:04 ` Jan-Benedict Glaw
2004-03-23 20:37 ` Glynn Clements
0 siblings, 2 replies; 8+ messages in thread
From: Mariano Moreyra @ 2004-03-23 14:52 UTC (permalink / raw)
To: 'Massimiliano Cialdi', 'linux-c-programming'
No... "char*" and "char []" are synonyms.
But in this case, you were having a warning because you were passing a
"const char *" to a function that expected a "char *" so the warning was
about the "const" qualifier.
If you have a code like this you wont have any warning at all
#include <stdio.h>
char *str = "pippo"; <---- Note there is no "const" quailfier anymore
int main(void)
{
void stampa(char s[]);
stampa(str);
return 0;
}
void stampa(char s[])
{
printf("%s\n", s);
}
So, "char[]" and "char*" are synonyms indeed.
-----Mensaje original-----
De: linux-c-programming-owner@vger.kernel.org
[mailto:linux-c-programming-owner@vger.kernel.org]En nombre de
Massimiliano Cialdi
Enviado el: Martes, 23 de Marzo de 2004 11:19
Para: linux-c-programming
Asunto: Re: const char * vs char[]
On Tue, 23 Mar 2004 10:44:36 -0300
"Mariano Moreyra" <moremari@aca.org.ar> wrote:
> I think that the problem is not a difference between "char*" and
> "char[]" The problem is that stampa is receiving a "char[]" and you
> are passing a"const char*" as the arg.
> The "qualifiers from pointer targer type" refers to the "const" part
> of the"const char*" declaration I belive.
> If you declare stampa like this:
>
> void stampa(const char s[]);
>
> you won't have this warning anymore.
so what is a synonym of char[]? Maybe "char (const *)"?
thanks
--
Massimiliano Cialdi
cialdi@firenze.net
m.cialdi@oksys.it
-
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: const char * vs char[]
2004-03-23 14:52 ` Mariano Moreyra
@ 2004-03-23 17:04 ` Jan-Benedict Glaw
2004-03-23 20:26 ` Glynn Clements
2004-03-23 20:37 ` Glynn Clements
1 sibling, 1 reply; 8+ messages in thread
From: Jan-Benedict Glaw @ 2004-03-23 17:04 UTC (permalink / raw)
To: 'linux-c-programming'
[-- Attachment #1: Type: text/plain, Size: 1035 bytes --]
On Tue, 2004-03-23 11:52:12 -0300, Mariano Moreyra <moremari@aca.org.ar>
wrote in message <000f01c410e6$6d7197e0$0c81640a@aca.org.ar>:
> No... "char*" and "char []" are synonyms.
There's a subtle difference:
char *hello_ptr = "A really loooong hello world";
char write_string[] = "this is some long garbage";
void main(void) {
/* legal */
sprintf (write_string, "%s", "stuff");
/* illegal - may be in a read-only segment */
sprintf (hello_ptr, "%s", "Don't do that");
}
So if you're preparing some space for say building up some packet (to be
sent out to hardware), use "char xx[]", but if you only want to have a
read-only string (to printf() it to the user), you can use "char *xx".
MfG, JBG
--
Jan-Benedict Glaw jbglaw@lug-owl.de . +49-172-7608481
"Eine Freie Meinung in einem Freien Kopf | Gegen Zensur | Gegen Krieg
fuer einen Freien Staat voll Freier Bürger" | im Internet! | im Irak!
ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: const char * vs char[]
2004-03-23 17:04 ` Jan-Benedict Glaw
@ 2004-03-23 20:26 ` Glynn Clements
0 siblings, 0 replies; 8+ messages in thread
From: Glynn Clements @ 2004-03-23 20:26 UTC (permalink / raw)
To: linux-c-programming
Jan-Benedict Glaw wrote:
> > No... "char*" and "char []" are synonyms.
>
> There's a subtle difference:
I wouldn't say that the difference between pointers and arrays is
subtle; the difference is actually rather substantial.
The reason why beginners tend to confuse the two is that C treats an
array as a pointer to its first element in almost all of the
situations where an array can be used.
> char *hello_ptr = "A really loooong hello world";
> char write_string[] = "this is some long garbage";
>
> void main(void) {
> /* legal */
> sprintf (write_string, "%s", "stuff");
>
> /* illegal - may be in a read-only segment */
> sprintf (hello_ptr, "%s", "Don't do that");
> }
>
> So if you're preparing some space for say building up some packet (to be
> sent out to hardware), use "char xx[]", but if you only want to have a
> read-only string (to printf() it to the user), you can use "char *xx".
In the latter case, you could also use "const char xx[]".
The main reasons to make a variable a pointer rather than an array
are:
1. You want the pointer to point at some pre-existing data.
2. You want to be able to change the pointer itself (as opposed to
what it points at).
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 8+ messages in thread
* RE: const char * vs char[]
2004-03-23 14:52 ` Mariano Moreyra
2004-03-23 17:04 ` Jan-Benedict Glaw
@ 2004-03-23 20:37 ` Glynn Clements
1 sibling, 0 replies; 8+ messages in thread
From: Glynn Clements @ 2004-03-23 20:37 UTC (permalink / raw)
To: mariano_moreyra
Cc: 'Massimiliano Cialdi', 'linux-c-programming'
Mariano Moreyra wrote:
> No... "char*" and "char []" are synonyms.
They aren't equivalent in the general case, only when declaring
function arguments.
> But in this case, you were having a warning because you were passing a
> "const char *" to a function that expected a "char *" so the warning was
> about the "const" qualifier.
> If you have a code like this you wont have any warning at all
>
> #include <stdio.h>
> char *str = "pippo"; <---- Note there is no "const" quailfier anymore
Whilst this may be legitimate from the perspective of the language
specification, it's less than ideal, as "str" is being initialised to
pointing at immutable storage.
With the above definition, the code:
*str = '\0';
won't generate a type error at compile time, but it will probably
segfault at run time (unless you used gcc's -fwritable-strings switch
(or the equivalent for whichever compiler you are using).
> void stampa(char s[])
> {
> printf("%s\n", s);
> }
Given that the array isn't being modified, the argument should be
declared with the "const" qualifier. Doing so would allow you to pass
a "const char *" or "const char []" to it without the need for an
explicit type cast.
More generally, the "const" qualifier should only be omitted when the
function can actually modify the data to which the argument points.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2004-03-23 20:37 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-03-23 12:01 const char * vs char[] Massimiliano Cialdi
2004-03-23 12:19 ` Glynn Clements
2004-03-23 13:44 ` Mariano Moreyra
2004-03-23 14:19 ` Massimiliano Cialdi
2004-03-23 14:52 ` Mariano Moreyra
2004-03-23 17:04 ` Jan-Benedict Glaw
2004-03-23 20:26 ` Glynn Clements
2004-03-23 20:37 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).