From: Jim Bancroft on
Hi everyone,

I'm using a function with the following signature:

void output_char(unsigned char c);

When I call it like so and compile, I get an error message which reads
"warning: passing arg 1 of `output_char' makes integer from pointer without
a cast.":

output_char("*");

However, if I enclose the character with single quotes instead of double, I
have no trouble. Is this to do with C/C++ treating a string as a pointer to
an array of characters? I'm a little confused as to what's happening and
was hoping someone could explain. Thanks!



From: R. Scott Mellow on
Jim Bancroft wrote:
> Hi everyone,
>
> I'm using a function with the following signature:
>
> void output_char(unsigned char c);
>
> When I call it like so and compile, I get an error message which reads
> "warning: passing arg 1 of `output_char' makes integer from pointer without
> a cast.":
>
> output_char("*");
>
> However, if I enclose the character with single quotes instead of double, I
> have no trouble. Is this to do with C/C++ treating a string as a pointer to
> an array of characters? I'm a little confused as to what's happening and
> was hoping someone could explain. Thanks!
>

Your guess is basically correct. I will attempt a more detailed explanation:

The type of your string literal "*" is const char[2], i.e. an array of
constant characters with two elements (one for the asterisk and one for
the terminating null character).

The type of the same character surrounded by single quotes, i.e. '*', is
simply char which is a type of integer.

When you use an array as a parameter to a function the language turns
that array into a pointer to the array's first element which is why the
error message mentioned a pointer.

--
Randy
From: Bart van Ingen Schenau on
Jim Bancroft wrote:

> Hi everyone,
>
> I'm using a function with the following signature:
>
> void output_char(unsigned char c);
>
> When I call it like so and compile, I get an error message which reads
> "warning: passing arg 1 of `output_char' makes integer from pointer
> without a cast.":
>
> output_char("*");
>
> However, if I enclose the character with single quotes instead of
> double, I
> have no trouble. Is this to do with C/C++ treating a string as a
> pointer to
> an array of characters?

That is exactly right.
In C and C++ a sequence of zero or more characters enclosed in double
quotes is a string-literal.
To be able to use the string-literal in the final executable, the
compiler will create an unnamed array that will hold the string-literal
as an C-style string (a sequence of characters that ends with the first
\0 character).
Because the normal conversion rules apply to this unnamed array, will
you see a pointer to the first character in most contexts where you use
the string-literal.

The reason that your code compiles successfully when you use '*', is
because the single quotes indicate a character-literal.
Character-literals are for situations where you (always) are dealing
with exactly one character.

> I'm a little confused as to what's happening
> and
> was hoping someone could explain. Thanks!

Bart v Ingen Schenau
--
a.c.l.l.c-c++ FAQ: http://www.comeaucomputing.com/learn/faq
c.l.c FAQ: http://www.eskimo.com/~scs/C-faq/top.html
c.l.c++ FAQ: http://www.parashift.com/c++-faq-lite/