public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* strings and char *
@ 2006-08-10 18:09 Michael Surette
  2006-08-10 18:10 ` Sven Eschenberg
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Michael Surette @ 2006-08-10 18:09 UTC (permalink / raw)
  To: gcc-help

It's been a while since I've written any C code, so please forgive me if
this is a newbie type question.

I was having a problem with a larger program when I ran into a problem.  The
following code snippet should work according to what I remember of C. 
What's wrong with it?

------------

char * color;
char * r="red";
color = r;

------------

gcc -c ar.c

------------

ar.c:3: error: conflicting types for 'color'
ar.c:1: error: previous declaration of 'color' was here
ar.c:3: warning: initialization makes integer from pointer without a cast
ar.c:3: error: initializer element is not constant
ar.c:3: warning: data definition has no type or storage class

------------

gcc --version

------------

gcc (GCC) 3.4.4
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: strings and char *
  2006-08-10 18:09 strings and char * Michael Surette
@ 2006-08-10 18:10 ` Sven Eschenberg
  2006-08-10 19:07 ` Bob Rossi
  2006-08-13 13:50 ` Luca Masini
  2 siblings, 0 replies; 8+ messages in thread
From: Sven Eschenberg @ 2006-08-10 18:10 UTC (permalink / raw)
  To: gcc-help

Hi Michael,

this is not really a gcc related question, but to shorten this: color 
and r differ in types, that's why you are getting an error.

Regards

-Sven


Michael Surette wrote:
> It's been a while since I've written any C code, so please forgive me if
> this is a newbie type question.
>
> I was having a problem with a larger program when I ran into a problem.  The
> following code snippet should work according to what I remember of C. 
> What's wrong with it?
>
> ------------
>
> char * color;
> char * r="red";
> color = r;
>
> ------------
>
> gcc -c ar.c
>
> ------------
>
> ar.c:3: error: conflicting types for 'color'
> ar.c:1: error: previous declaration of 'color' was here
> ar.c:3: warning: initialization makes integer from pointer without a cast
> ar.c:3: error: initializer element is not constant
> ar.c:3: warning: data definition has no type or storage class
>
> ------------
>
> gcc --version
>
> ------------
>
> gcc (GCC) 3.4.4
> Copyright (C) 2004 Free Software Foundation, Inc.
> This is free software; see the source for copying conditions.  There is NO
> warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
>
>   

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: strings and char *
  2006-08-10 18:09 strings and char * Michael Surette
  2006-08-10 18:10 ` Sven Eschenberg
@ 2006-08-10 19:07 ` Bob Rossi
  2006-08-10 19:12   ` Michael Surette
  2006-08-13 13:50 ` Luca Masini
  2 siblings, 1 reply; 8+ messages in thread
From: Bob Rossi @ 2006-08-10 19:07 UTC (permalink / raw)
  To: Michael Surette; +Cc: gcc-help

On Thu, Aug 10, 2006 at 02:00:59PM -0400, Michael Surette wrote:
> It's been a while since I've written any C code, so please forgive me if
> this is a newbie type question.
> 
> I was having a problem with a larger program when I ran into a problem.  The
> following code snippet should work according to what I remember of C. 
> What's wrong with it?
> 
> ------------
> 
> char * color;
> char * r="red";
> color = r;

Is that at the global scope? You can't make assignments in the global
scope unless they are definitions as well. Try putting that code in a
function scope.

Bob Rossi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: strings and char *
  2006-08-10 19:07 ` Bob Rossi
@ 2006-08-10 19:12   ` Michael Surette
  2006-08-10 19:18     ` Bob Rossi
  0 siblings, 1 reply; 8+ messages in thread
From: Michael Surette @ 2006-08-10 19:12 UTC (permalink / raw)
  To: gcc-help

Bob Rossi wrote:

> On Thu, Aug 10, 2006 at 02:00:59PM -0400, Michael Surette wrote:
>> It's been a while since I've written any C code, so please forgive me if
>> this is a newbie type question.
>> 
>> I was having a problem with a larger program when I ran into a problem. 
>> The following code snippet should work according to what I remember of C.
>> What's wrong with it?
>> 
>> ------------
>> 
>> char * color;
>> char * r="red";
>> color = r;
> 
> Is that at the global scope? You can't make assignments in the global
> scope unless they are definitions as well. Try putting that code in a
> function scope.
> 
> Bob Rossi

Thanks for the quick response.  That fixed it.  That would be one of those
many changes to the C language over the years I guess.  What threw me off
was that it compiled fine for my brother, who runs a Windows based
compiler.

As I mentioned in my original posting, I wrote that snippet because I was
having a problem with a larger program, a glue program between lua and
sendmail's milter interface.  Perhaps you can help me with that?

I have a function defined as...

static sfsistat callback(SMFICTX *ctx, char *cbname, char *cbargs[]);

later in the code, I call it (one of many times)...

return callback(ctx, "helo", {helohost,NULL});

where...

ctx is a SMFICTX * supplied by sendmail
"helo" is obviously a character string
helohost is a char * provided by sendmail

When I try to compile I get the error message...

luamilter.c:137: error: parse error before '{' token

Is this another of those many changes over the years that I missed?

Mike



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: strings and char *
  2006-08-10 19:12   ` Michael Surette
@ 2006-08-10 19:18     ` Bob Rossi
  2006-08-10 20:10       ` Michael Surette
  2006-08-11  9:39       ` Shriramana Sharma
  0 siblings, 2 replies; 8+ messages in thread
From: Bob Rossi @ 2006-08-10 19:18 UTC (permalink / raw)
  To: Michael Surette; +Cc: gcc-help

On Thu, Aug 10, 2006 at 03:07:08PM -0400, Michael Surette wrote:
> Bob Rossi wrote:
> 
> > On Thu, Aug 10, 2006 at 02:00:59PM -0400, Michael Surette wrote:
> >> It's been a while since I've written any C code, so please forgive me if
> >> this is a newbie type question.
> >> 
> >> I was having a problem with a larger program when I ran into a problem. 
> >> The following code snippet should work according to what I remember of C.
> >> What's wrong with it?
> >> 
> >> ------------
> >> 
> >> char * color;
> >> char * r="red";
> >> color = r;
> > 
> > Is that at the global scope? You can't make assignments in the global
> > scope unless they are definitions as well. Try putting that code in a
> > function scope.
> > 
> > Bob Rossi
> 
> Thanks for the quick response.  That fixed it.  That would be one of those
> many changes to the C language over the years I guess.  What threw me off
> was that it compiled fine for my brother, who runs a Windows based
> compiler.
> 
> As I mentioned in my original posting, I wrote that snippet because I was
> having a problem with a larger program, a glue program between lua and
> sendmail's milter interface.  Perhaps you can help me with that?
> 
> I have a function defined as...
> 
> static sfsistat callback(SMFICTX *ctx, char *cbname, char *cbargs[]);
> 
> later in the code, I call it (one of many times)...
> 
> return callback(ctx, "helo", {helohost,NULL});

I'm just guessing, but if you do
  char *tmp[] = {"helohost", NULL};
and then call it like
  return callback(ctx, "helo", tmp);
it might work.

This might be off topic for the gcc mailing list.

Bob Rossi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: strings and char *
  2006-08-10 19:18     ` Bob Rossi
@ 2006-08-10 20:10       ` Michael Surette
  2006-08-11  9:39       ` Shriramana Sharma
  1 sibling, 0 replies; 8+ messages in thread
From: Michael Surette @ 2006-08-10 20:10 UTC (permalink / raw)
  To: gcc-help


> 
> I'm just guessing, but if you do
>   char *tmp[] = {"helohost", NULL};
> and then call it like
>   return callback(ctx, "helo", tmp);
> it might work.
> 
> This might be off topic for the gcc mailing list.
> 
> Bob Rossi

ok, I try that.  Thanks for the help.

Mike

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: strings and char *
  2006-08-10 19:18     ` Bob Rossi
  2006-08-10 20:10       ` Michael Surette
@ 2006-08-11  9:39       ` Shriramana Sharma
  1 sibling, 0 replies; 8+ messages in thread
From: Shriramana Sharma @ 2006-08-11  9:39 UTC (permalink / raw)
  To: gcc-help

On 8/11/06, Bob Rossi <bob_rossi@cox.net> wrote:

> This might be off topic for the gcc mailing list.

http://vger.kernel.org/vger-lists.html#linux-c-programming would be a
better place for general problems with C programming. A very helpful
list. [Just like this one ;)]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: strings and char *
  2006-08-10 18:09 strings and char * Michael Surette
  2006-08-10 18:10 ` Sven Eschenberg
  2006-08-10 19:07 ` Bob Rossi
@ 2006-08-13 13:50 ` Luca Masini
  2 siblings, 0 replies; 8+ messages in thread
From: Luca Masini @ 2006-08-13 13:50 UTC (permalink / raw)
  To: Michael Surette; +Cc: gcc-help

Michael Surette wrote:

> I was having a problem with a larger program when I ran into a problem.  The
> following code snippet should work according to what I remember of C. 
> What's wrong with it?
> 
> ------------
> 
> char * color;
> char * r="red";
> color = r;

You cannot do that in file scope.

Inside a function it works:

void f( void )
{
     char * color;
     char * r = "red";

     color = r;
}


Regards.

Luca Masini

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2006-08-11 22:07 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-08-10 18:09 strings and char * Michael Surette
2006-08-10 18:10 ` Sven Eschenberg
2006-08-10 19:07 ` Bob Rossi
2006-08-10 19:12   ` Michael Surette
2006-08-10 19:18     ` Bob Rossi
2006-08-10 20:10       ` Michael Surette
2006-08-11  9:39       ` Shriramana Sharma
2006-08-13 13:50 ` Luca Masini

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 read-only IMAP folder(s) and NNTP newsgroup(s).