Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Returning a pointer to a constant string

Reply
Thread Tools

Returning a pointer to a constant string

 
 
=?iso-8859-1?q?Santiago_Urue=F1a?=
Guest
Posts: n/a
 
      09-17-2007
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

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      09-17-2007
In article <(E-Mail Removed). com>,
=?iso-8859-1?q?Santiago_Urue=F1a?= <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
JimS
Guest
Posts: n/a
 
      09-17-2007
On Mon, 17 Sep 2007 15:38:15 -0700, Santiago Urueña
<(E-Mail Removed)> 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
 
Reply With Quote
 
=?iso-8859-1?q?Santiago_Urue=F1a?=
Guest
Posts: n/a
 
      09-17-2007
> > 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

 
Reply With Quote
 
=?iso-8859-1?q?Santiago_Urue=F1a?=
Guest
Posts: n/a
 
      09-17-2007
> 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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-17-2007
Santiago Urueña <(E-Mail Removed)> 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 <(E-Mail Removed)> 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) http://www.velocityreviews.com/forums/(E-Mail Removed) <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"
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      09-17-2007
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

 
Reply With Quote
 
Martin Ambuhl
Guest
Posts: n/a
 
      09-18-2007
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;
}
 
Reply With Quote
 
anders
Guest
Posts: n/a
 
      09-18-2007
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 <(E-Mail Removed)> 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



 
Reply With Quote
 
Charlie Gordon
Guest
Posts: n/a
 
      09-18-2007
"Walter Roberson" <(E-Mail Removed)-cnrc.gc.ca> a écrit dans le message de
news: fcn02v$2j9$(E-Mail Removed)...
> In article <(E-Mail Removed). com>,
> =?iso-8859-1?q?Santiago_Urue=F1a?= <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
concatenate a constant to constant string using macros sinbad C Programming 7 06-19-2008 05:28 PM
len(var) is [CONSTANT] equal to len(var) == [CONSTANT]? Tor Erik Soenvisen Python 14 11-23-2006 09:57 PM
"Non-constant" constant can't be used as template argument Martin Magnusson C++ 2 10-08-2004 08:41 AM
Understanding How To Use #ifdef Constant #define Constant Sequence In Multible Files Christopher M. Lusardi C++ 1 09-02-2004 07:43 AM
pointer to member function and pointer to constant member function Fraser Ross C++ 4 08-14-2004 06:00 PM



Advertisments