public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* "char *" diff in header & func arg list?
@ 2007-03-26 18:09 kourama
  2007-03-26 18:26 ` John Love-Jensen
  0 siblings, 1 reply; 3+ messages in thread
From: kourama @ 2007-03-26 18:09 UTC (permalink / raw)
  To: gcc-help


I have noticed something that seems wrong to me, and I'd like to know if
anyone can confirm the bug or explain the seemingly strange behaviour.
Attached is a set of files which demonstrates the problem:

http://www.nabble.com/file/7426/a.c a.c 
http://www.nabble.com/file/7427/b.c b.c 
http://www.nabble.com/file/7428/Makefile Makefile 

When the aa function in a.c declares "char *arg" in it's argument list, it
gets a pointer, which means that "char *arg" is equivalent to "char arg[]"
in the argument list.

However, in the file b.c, the declaration "extern char *myarray" is treated
differently from "extern char myarray[]"

It seems to me that, based on the context, the two should be equivalent, but
GCC doesn't treat them that way.

So, is this a bug, legacy behaviour or is this just another wrinkle in the
sharpei puppy that is C?

Many thanks.
-- 
View this message in context: http://www.nabble.com/%22char-*%22-diff-in-header---func-arg-list--tf3468423.html#a9677694
Sent from the gcc - Help mailing list archive at Nabble.com.

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

* Re: "char *" diff in header & func arg list?
  2007-03-26 18:09 "char *" diff in header & func arg list? kourama
@ 2007-03-26 18:26 ` John Love-Jensen
  2007-03-26 23:40   ` kourama
  0 siblings, 1 reply; 3+ messages in thread
From: John Love-Jensen @ 2007-03-26 18:26 UTC (permalink / raw)
  To: kourama, MSX to GCC

Hi kourama,

These two things are mostly interchangeable for most purposes as a
parameter:

char array[];
char* pointer;

On the caller's side, arg1 degenerates into something that almost is the
same as:

char* const arg1;

C proponents would say this is a good thing.  I've been working in C++ long
enough to consider it a bit of a wart (but C++ has far more atrocious warts
than this to worry about), as part of the C legacy.


However, this is an outright mismatch:

extern char array[];
extern char* pointer;

char array[] = "good";
char pointer[] = "bad";

Why?  Because an array of characters is not a pointer to an array of
characters.  The declaration does not match the definition.

> So, is this a bug, legacy behaviour or is this just another wrinkle in the
sharpei puppy that is C?

In this case, not a wrinkle, not a "legacy C" issue.  It is a PEBKAC bug.

HTH,
--Eljay

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

* Re: "char *" diff in header & func arg list?
  2007-03-26 18:26 ` John Love-Jensen
@ 2007-03-26 23:40   ` kourama
  0 siblings, 0 replies; 3+ messages in thread
From: kourama @ 2007-03-26 23:40 UTC (permalink / raw)
  To: gcc-help




John Love-Jensen wrote:
> 
> Why?  Because an array of characters is not a pointer to an array of
> characters.  The declaration does not match the definition.
> 

Ah, of course. I was effectly casting the array to a pointer type in the
extern declaration.

Thanks.

Honestly, am I dim or is that a subtle "gotcha"?

eh...don't answer that.
-- 
View this message in context: http://www.nabble.com/%22char-*%22-diff-in-header---func-arg-list--tf3468423.html#a9678711
Sent from the gcc - Help mailing list archive at Nabble.com.

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

end of thread, other threads:[~2007-03-26 18:26 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-03-26 18:09 "char *" diff in header & func arg list? kourama
2007-03-26 18:26 ` John Love-Jensen
2007-03-26 23:40   ` kourama

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).