char *str vs. char s 
Author Message
 char *str vs. char s

In a message to All <04-09-93 09:34>, Dave Eisen writes:


 DE> 06:24:36 GMT Organization: Sequoia Peripherals, Inc. Message-ID:

 DE>


 DE>
 DE> >Even in initializing a string constant (the way I understand it)
 DE> >'char str[] = "hello";', str is just a pointer to the space somewhere
 DE> >else containing the string.
 DE>
 DE> No. str is an array. The declaration you gave is completely synonymous
 DE> with
 DE>
 DE> char str[6] = "hello";
 DE>
 DE> or
 DE>
 DE> char str[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
 DE>
 DE> That is, it declares str to be a character array, not a pointer.
 DE>
 DE> If you want str to be a pointer to the beginning of an anonymous
 DE> array of characters which spell out "hello", then declare str to be a
 DE> pointer. This is done as follows:
 DE>
 DE> char *str = "hello";
 DE>
 DE> A pointer is not an array and an array is not a pointer.
 DE>
 DE>
 DE>
 DE> --  Dave Eisen                               "To succeed in the world,

 DE> stupid, you must also Sequoia Peripherals: (415) 967-5644       be
 DE> well-mannered." --- Voltaire Home:                (415) 321-5154   r
 DE>
 DE>
 DE> --- QM v1.30
 DE>  * Origin: (GIS)Atlanta96(GO BRAVES) (1:133/411.0)
 DE>
 DE>
 DE>

Dave,
 I do not agree.  In reading a reference on ANSI C it states the following:

 If a string constant appears anywhere except as an argument to sizeof or as
an initializer of a character array, the conversions that are usually
applied to arrays come into play, and these conversions change the string
from an array of characters to a pointer to the first character in the
string, which has a type of char * or pointer to char.

Does anyone else disagree?

Richard

|---M----a----x----#----T----a----g----L----i----n----e---|
  ___  
 * PMQWK 32! 1.00b7b *

--  
                 __
FidoNet<==>UUCP /  \  UUCP: wittsend!gisatl!308!Richard.L..Taylor.Jr.

              (_|  /_)        

Administrator |     | \   \\ (GIS) Atlanta
(404)252-1699 | (*) |  \_  ))
    ______    |__U__| /  \//   User: Richard L. Taylor Jr.
   / FIDO \    _//|| _\   /    Via FidoNet Node: 1:133/411.0
  (________)  (_/(_|(____/ (jm)  



Thu, 28 Sep 1995 04:22:00 GMT  
 char *str vs. char s

Quote:
> In a message to All <04-09-93 09:34>, Dave Eisen writes:

>  DE> 06:24:36 GMT Organization: Sequoia Peripherals, Inc. Message-ID:

>  DE>


>  DE>
>  DE> >Even in initializing a string constant (the way I understand it)
>  DE> >'char str[] = "hello";', str is just a pointer to the space somewhere
>  DE> >else containing the string.
>  DE>
>  DE> No. str is an array. The declaration you gave is completely synonymous
>  DE> with
>  DE>
>  DE> char str[6] = "hello";
>  DE>
>  DE> or
>  DE>
>  DE> char str[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
>  DE>
>  DE> That is, it declares str to be a character array, not a pointer.
>  DE>
>  DE> If you want str to be a pointer to the beginning of an anonymous
>  DE> array of characters which spell out "hello", then declare str to be a
>  DE> pointer. This is done as follows:
>  DE>
>  DE> char *str = "hello";
>  DE>
>  DE> A pointer is not an array and an array is not a pointer.
>  DE>
>  DE>
>  DE>
>  DE> --  Dave Eisen                               "To succeed in the world,

>  DE> stupid, you must also Sequoia Peripherals: (415) 967-5644       be
>  DE> well-mannered." --- Voltaire Home:                (415) 321-5154   r
> Dave,
>  I do not agree.  In reading a reference on ANSI C it states the following:
>  If a string constant appears anywhere except as an argument to sizeof or as
> an initializer of a character array, the conversions that are usually
> applied to arrays come into play, and these conversions change the string
> from an array of characters to a pointer to the first character in the
> string, which has a type of char * or pointer to char.
> Does anyone else disagree?

I don't think that's at all inconsistent with what dave said.  The point is
that there is a conversion -- they are not the same.  This implies that the
programmer must make the correct declarations so the compile knows to do
the conversion.

If in one file you define

        char str[] = "hello";

str is an array.  If in another you declare

        extern char *str;

It will not work because str is not a pointer.  It is an array which should in
most cases be converted to a pointer, but only if the compiler knows that it
is an array.

This isn't really a unique situation.  In most expressions a char is promoted
to an int.  Again, the compiler must know to do the conversion.  If in one
file you define

        char ch;

and in another you declare

        extern int ch;

nothing good will happen, unless you are working on a machine on which char
and int have the same size and format.  On most machines you will get
a protection fault or will use the wrong value for ch.

--
Mike Rubenstein



Fri, 29 Sep 1995 10:17:42 GMT  
 char *str vs. char s

Quote:
>In a message to All <04-09-93 09:34>, Dave Eisen writes:


> DE>
> DE> >Even in initializing a string constant (the way I understand it)
> DE> >'char str[] = "hello";', str is just a pointer to the space somewhere
> DE> >else containing the string.
> DE>
> DE> No. str is an array. The declaration you gave is completely synonymous
> DE> with
> DE>
> DE> char str[6] = "hello";
> DE>
> DE> or
> DE>
> DE> char str[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
> DE>
> DE> That is, it declares str to be a character array, not a pointer.

Right.

Quote:
>Dave,
> I do not agree.  In reading a reference on ANSI C it states the following:
> If a string constant appears anywhere except as an argument to sizeof or as
>an initializer of a character array, [...]

 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

That *is* an initializer of a character array!

Sorry about the quote:new ratio, but sometimes a man's gotta do what a
man's gotta do!  :-)
--
Chris Waters    | the insane don't |"Judy's in the bedroom,



Fri, 29 Sep 1995 11:33:35 GMT  
 char *str vs. char s

Quote:
>In a message to All <04-09-93 09:34>, Dave Eisen writes:


> DE>
> DE> >Even in initializing a string constant (the way I understand it)
> DE> >'char str[] = "hello";', str is just a pointer to the space somewhere
> DE> >else containing the string.
> DE>
> DE> No. str is an array. The declaration you gave is completely synonymous
> DE> with
> DE>
> DE> char str[6] = "hello";
> DE>
> DE> or
> DE>
> DE> char str[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
> DE>
> DE> That is, it declares str to be a character array, not a pointer.
> DE>
> DE> If you want str to be a pointer to the beginning of an anonymous
> DE> array of characters which spell out "hello", then declare str to be a
> DE> pointer. This is done as follows:
> DE>
> DE> char *str = "hello";
> DE>
> DE> A pointer is not an array and an array is not a pointer.
>Dave,
> I do not agree.  In reading a reference on ANSI C it states the following:
> If a string constant appears anywhere except as an argument to sizeof or as
>an initializer of a character array, the conversions that are usually

 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Quote:
>applied to arrays come into play, and these conversions change the string
>from an array of characters to a pointer to the first character in the
>string, which has a type of char * or pointer to char.

The example that Dave was refering too is one of the execptions mentione,
i.e. in

char str[] = "Hello";

"Hello" *is* the initializer of a character array and therefore str is
*not* converted to a pointer.

-patsch

--
"Electrical Engineers do it with Frequency until it Hertz."



Fri, 29 Sep 1995 11:57:44 GMT  
 char *str vs. char s

Quote:
>In a message to All <04-09-93 09:34>, Dave Eisen writes:


> DE> 06:24:36 GMT Organization: Sequoia Peripherals, Inc. Message-ID:

> DE>


> DE>
> DE> >Even in initializing a string constant (the way I understand it)
> DE> >'char str[] = "hello";', str is just a pointer to the space somewhere
> DE> >else containing the string.
> DE>
> DE> No. str is an array. The declaration you gave is completely synonymous
> DE> with
> DE>
> DE> char str[6] = "hello";
> DE>
> DE> or
> DE>
> DE> char str[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
> DE>
> DE> That is, it declares str to be a character array, not a pointer.
> DE>
> DE> If you want str to be a pointer to the beginning of an anonymous
> DE> array of characters which spell out "hello", then declare str to be a
> DE> pointer. This is done as follows:
> DE>
> DE> char *str = "hello";
> DE>
> DE> A pointer is not an array and an array is not a pointer.
> DE>
> DE>
> DE>
> DE> --  Dave Eisen                               "To succeed in the world,

> DE> stupid, you must also Sequoia Peripherals: (415) 967-5644       be
> DE> well-mannered." --- Voltaire Home:                (415) 321-5154   r
> DE>
> DE>
> DE> --- QM v1.30
> DE>  * Origin: (GIS)Atlanta96(GO BRAVES) (1:133/411.0)
> DE>
> DE>
> DE>

>Dave,
> I do not agree.  In reading a reference on ANSI C it states the following:

> If a string constant appears anywhere except as an argument to sizeof or as
>an initializer of a character array, the conversions that are usually
>applied to arrays come into play, and these conversions change the string
>from an array of characters to a pointer to the first character in the
>string, which has a type of char * or pointer to char.

>Does anyone else disagree?

I agree with Dave, citing your quoted reference as my reference.  The
code:

char str[] = "hello";

sure seems like it falls under the category of "an initializer of a
character array", which is an exception to the automatic conversions of
string constants to pointers to chars, as quoted by you.

The code:

char *str = "hello";

doesn't fall into that class, so "hello" is converted to a pointer to
the 'h'.

- Show quoted text -

Quote:
>Richard

>|---M----a----x----#----T----a----g----L----i----n----e---|
>  ___  
> * PMQWK 32! 1.00b7b *

>--  
>                 __
>FidoNet<==>UUCP /  \  UUCP: wittsend!gisatl!308!Richard.L..Taylor.Jr.

>              (_|  /_)        

>Administrator |     | \   \\ (GIS) Atlanta
>(404)252-1699 | (*) |  \_  ))
>    ______    |__U__| /  \//   User: Richard L. Taylor Jr.
>   / FIDO \    _//|| _\   /    Via FidoNet Node: 1:133/411.0
>  (________)  (_/(_|(____/ (jm)  

--
-----

(insert-file ".disclaimer")
"I'm not an actor, but I play one on TV."


Fri, 29 Sep 1995 12:33:36 GMT  
 char *str vs. char s

|> In a message to All <04-09-93 09:34>, Dave Eisen writes:

[Correct stuff from Dave deleted]

|> Dave,
|>  I do not agree.  In reading a reference on ANSI C it states the following:
|>
|>  If a string constant appears anywhere except as an argument to sizeof or as
|> an initializer of a character array, the conversions that are usually
|> applied to arrays come into play, and these conversions change the string
|> from an array of characters to a pointer to the first character in the
|> string, which has a type of char * or pointer to char.
|>
|> Does anyone else disagree?
|>
|> Richard

I don't understand. Dave says "One plus one is two", and you say, "I disagree.
Two plus two is four."

There's no contradiction between what you said and what Dave said. Use of a
string literal in an expression (not in an initialization) behaves exactly as
if you used an array lvalue in the expression, with the exception that in the
string literal case, the contents are not modifiable. That's essentially what
the reference you quoted says.

--
==================
Chris Volpe
G.E. Corporate R&D



Sat, 30 Sep 1995 03:16:34 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. char **str vs. char *str[]

2. char *str vs. char str[]

3. char* and char str[2]

4. A char pointer (char *) vs. char array question

5. char[] vs char * vs #defines for constant strings

6. char *c vs. char c[]

7. char * vs char[]

8. char ** vs char * f()

9. char [] vs char *

10. char ** VS char *** ???

11. char *line vs. char line[]

12. const char* vs. char *

 

 
Powered by phpBB® Forum Software