Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Returning a pointer to a constant string (http://www.velocityreviews.com/forums/t538020-returning-a-pointer-to-a-constant-string.html)

=?iso-8859-1?q?Santiago_Urue=F1a?= 09-17-2007 10:38 PM

Returning a pointer to a constant string
 
Hi,

I tried to return a pointer to a constant string, but the compiler
gives the following warning if a cast is not used:

warning: assignment from incompatible pointer type

This is the code:


const char msg[] = "Test message";

const char *message(void) {
return msg;
}

int main(void){
const char * str;

str = (const char *)message;
str = (char *)message;
str = message; /* GCC warning! */

str = (const char *)msg;
str = (char *)msg;
str = msg;

return 0;
}

Oddly, GCC only gives the warning if no cast is used, but it doesn't
complain if the cast discards the const qualifier. Is this behavior
OK? I'm using GCC 4.1.2. Thanks!

Best regards,

Santi


Walter Roberson 09-17-2007 10:45 PM

Re: Returning a pointer to a constant string
 
In article <1190068695.241389.292540@g4g2000hsf.googlegroups. com>,
=?iso-8859-1?q?Santiago_Urue=F1a?= <suruena@gmail.com> wrote:

>I tried to return a pointer to a constant string, but the compiler
>gives the following warning if a cast is not used:


> warning: assignment from incompatible pointer type


>This is the code:


>const char msg[] = "Test message";


>const char *message(void) {
> return msg;
>}


Pay close attention to the placement of the const qualifiers.

const char msg[] says that msg[someindex] will be a const char

const char *message(void)

says that message will return a pointer to a char and that the
pointer is constant. (I think. I'm not -positive-. I haven't had
much occasion to use const.)
--
All is vanity. -- Ecclesiastes

JimS 09-17-2007 10:57 PM

Re: Returning a pointer to a constant string
 
On Mon, 17 Sep 2007 15:38:15 -0700, Santiago Urueña
<suruena@gmail.com> wrote:

>Hi,
>
>I tried to return a pointer to a constant string, but the compiler
>gives the following warning if a cast is not used:
>
> warning: assignment from incompatible pointer type
>
>This is the code:
>
>
>const char msg[] = "Test message";
>
>const char *message(void) {
> return msg;
>}
>
>int main(void){
> const char * str;
>
> str = (const char *)message;
> str = (char *)message;
> str = message; /* GCC warning! */


message is a pointer to a function returning const char *. The
previous two casts are covering up a gratuitous mistake!

Jim

=?iso-8859-1?q?Santiago_Urue=F1a?= 09-17-2007 11:06 PM

Re: Returning a pointer to a constant string
 
> > str = (const char *)message;
> > str = (char *)message;
> > str = message; /* GCC warning! */

>
> message is a pointer to a function returning const char *. The
> previous two casts are covering up a gratuitous mistake!
>

You are right! Silly me.

Anyway, the compiler doesn't give any warning even if the qualifier is
discarded by the cast:

str = (const char *)message();
str = (char *)message(); /* No warning! */
str = message();

Is this OK?

Thanks again.

Santi


=?iso-8859-1?q?Santiago_Urue=F1a?= 09-17-2007 11:18 PM

Re: Returning a pointer to a constant string
 
> Anyway, the compiler doesn't give any warning even if the qualifier is
> discarded by the cast:
>
> str = (const char *)message();
> str = (char *)message(); /* No warning! */
> str = message();
>
> Is this OK?
>

After thinking a little more about this, I think I have the answer:
the compiler shouldn't give a warning because a pointer to a non-const
object is being assigned to a pointer to a const object, and this is
totally OK because (anyway you cannot modify the object via the 'str'
pointer).

Thanks


Keith Thompson 09-17-2007 11:44 PM

Re: Returning a pointer to a constant string
 
Santiago Urueña <suruena@gmail.com> writes:
>> > str = (const char *)message;
>> > str = (char *)message;
>> > str = message; /* GCC warning! */

>>
>> message is a pointer to a function returning const char *. The
>> previous two casts are covering up a gratuitous mistake!
>>

> You are right! Silly me.
>
> Anyway, the compiler doesn't give any warning even if the qualifier is
> discarded by the cast:
>
> str = (const char *)message();
> str = (char *)message(); /* No warning! */
> str = message();
>
> Is this OK?


Please leave attribution lines in place for quoted text (i.e., lines
like "So-and-so <foo@bar.com> writes:"). They make it easier to
follow the conversation, and it's just polite to credit people for
their words.

A cast specifies a type conversion, but what it *really* does is tell
the compiler "I know exactly what I'm doing, don't bother me with
warnings". Because of that property, almost all casts should be
viewed with suspicion. Adding a cast for the sole purpose of
silencing a compiler warning is almost always a mistake; the correct
solution is usually to fix the code so the cast isn't required, either
by arranging for things to be of the desired type in the first place
or by using types that are converted implicitly.

One of the few cases where a cast is necessary is for some arguments
to variadic functions like printf(). For non-variadic functions, the
compiler knows the required type and is able to generate an implicit
conversion if necessary. For a variadic function, the compiler
doesn't necessarily have this information, so you have to give it some
help.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

CBFalconer 09-17-2007 11:49 PM

Re: Returning a pointer to a constant string
 
Santiago Urueña wrote:
>
> I tried to return a pointer to a constant string, but the compiler
> gives the following warning if a cast is not used:
>
> warning: assignment from incompatible pointer type
>
> This is the code:
>
> const char msg[] = "Test message";
>
> const char *message(void) {
> return msg;
> }


msg is an array. &msg is a pointer to msg.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>


--
Posted via a free Usenet account from http://www.teranews.com


Martin Ambuhl 09-18-2007 06:04 AM

Re: Returning a pointer to a constant string
 
Santiago Urueña wrote:
> Hi,
>
> I tried to return a pointer to a constant string, but the compiler
> gives the following warning if a cast is not used:
>
> warning: assignment from incompatible pointer type
>
> This is the code:
>
>
> const char msg[] = "Test message";
>
> const char *message(void) {
> return msg;
> }
>
> int main(void){
> const char * str;
>
> str = (const char *)message;
> str = (char *)message;
> str = message; /* GCC warning! */
>
> str = (const char *)msg;
> str = (char *)msg;
> str = msg;
>
> return 0;
> }


const char msg[] = "Test message";
/* msg (above) is a character array, but message is a function */
const char *message(void)
{
return msg;
}

int main(void)
{
const char *str;
str = message(); /* notice parenthesis; message is not a
pointer-to-char but a function
returning a (const) pointer-to-char */
str = msg;
return 0;
}

anders 09-18-2007 08:20 AM

Re: Returning a pointer to a constant string
 
Hmm as i see it
one type is a char [] and one is char *
that way
str = (char *)message();
works.
// Anders


On 18 Sep, 01:49, CBFalconer <cbfalco...@yahoo.com> wrote:
> Santiago Urueña wrote:
>
> > I tried to return a pointer to a constant string, but the compiler
> > gives the following warning if a cast is not used:

>
> > warning: assignment from incompatible pointer type

>
> > This is the code:

>
> > const char msg[] = "Test message";

>
> > const char *message(void) {
> > return msg;
> > }

>
> msg is an array. &msg is a pointer to msg.
>
> --
> Chuck F (cbfalconer at maineline dot net)
> Available for consulting/temporary embedded and systems.
> <http://cbfalconer.home.att.net>
>
> --
> Posted via a free Usenet account fromhttp://www.teranews.com




Charlie Gordon 09-18-2007 10:38 AM

Re: Returning a pointer to a constant string
 
"Walter Roberson" <roberson@ibd.nrc-cnrc.gc.ca> a écrit dans le message de
news: fcn02v$2j9$1@canopus.cc.umanitoba.ca...
> In article <1190068695.241389.292540@g4g2000hsf.googlegroups. com>,
> =?iso-8859-1?q?Santiago_Urue=F1a?= <suruena@gmail.com> wrote:
>
>>I tried to return a pointer to a constant string, but the compiler
>>gives the following warning if a cast is not used:

>
>> warning: assignment from incompatible pointer type

>
>>This is the code:

>
>>const char msg[] = "Test message";

>
>>const char *message(void) {
>> return msg;
>>}

>
> Pay close attention to the placement of the const qualifiers.
>
> const char msg[] says that msg[someindex] will be a const char


correct. It could also be written char const msg[].

> const char *message(void)
>
> says that message will return a pointer to a char and that the
> pointer is constant.


No, such a pointer would be defined as char * const p; and it makes no sense
as a return value for a function.

> (I think. I'm not -positive-. I haven't had much occasion to use const.)


It shows!
Please don't post misleading answers on subjects you know you don't master.

--
Chqrlie.




All times are GMT. The time now is 10:56 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.