Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   [pointers and arrays]: The difference between an array name and a pointer (http://www.velocityreviews.com/forums/t444617-pointers-and-arrays-the-difference-between-an-array-name-and-a-pointer.html)

iamchiaweilin@gmail.com 10-02-2006 04:21 PM

[pointers and arrays]: The difference between an array name and a pointer
 
Hello all:

What's the difference between p and q in the following statements?


char p[] = "Hello";
char *q = "Hello";

I know q stores the address of 'H'.
Question is: does p store the address of 'H' too?
I know p is the name of the array that contains "Hello". Is array name
a pointer?
In other words, is p exactly the same as &p[0]?

p and q are the same if you want to print them out by %s.
Is there a case where p and q (array name and pointer to the array)
can not be used interchangably? I knew there is, such as sizeof( ).

p.s. I remembered reading somewhere on the Net says that the statement
char *q = "Hello"
is not a good style of programming. coz you do not know whether q
points to a valid address or not.
Is it true?


pemo 10-02-2006 04:41 PM

Re: [pointers and arrays]: The difference between an array name and a pointer
 
iamchiaweilin@gmail.com wrote:
> Hello all:
>
> What's the difference between p and q in the following statements?
>
>
> char p[] = "Hello";
> char *q = "Hello";
>
> I know q stores the address of 'H'.
> Question is: does p store the address of 'H' too?
> I know p is the name of the array that contains "Hello". Is array name
> a pointer?
> In other words, is p exactly the same as &p[0]?
>
> p and q are the same if you want to print them out by %s.
> Is there a case where p and q (array name and pointer to the array)
> can not be used interchangably? I knew there is, such as sizeof( ).


One difference is that *q = ? [or q[0] = ???] may not work - as "Hello" here
may *not* be writeable [readonly] - however *p = ? and p[0] = ? will work -
it's shorthand for char p[] = { 'H', 'e', ... };, i.e., simple array
initialisation. Anyway, yes, p stores the address of *its* 'H' as does q.

> p.s. I remembered reading somewhere on the Net says that the statement
> char *q = "Hello"
> is not a good style of programming. coz you do not know whether q
> points to a valid address or not.
> Is it true?


No - if the code compiles, and there isn't some catastrophic runtime error,
then q points to the address where 'H' is stored.



Lew Pitcher 10-02-2006 05:04 PM

Re: [pointers and arrays]: The difference between an array name and a pointer
 
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


iamchiaweilin@gmail.com wrote:
> Hello all:
>
> What's the difference between p and q in the following statements?
>
>
> char p[] = "Hello";
> char *q = "Hello";
>
> I know q stores the address of 'H'.
> Question is: does p store the address of 'H' too?


No. p stores an array 6 of characters.


> I know p is the name of the array that contains "Hello". Is array name
> a pointer?


No, but it can often be treated /as if/ it were a pointer.

> In other words, is p exactly the same as &p[0]?


It depends on the context.

sizeof(p) will return 6 (the length of the reserved space), and
sizeof (&p[0]) will return the size of a character pointer

but

*p is equal to 'H', and
*(&p[0]) is also equal to 'H'

> p and q are the same if you want to print them out by %s.
> Is there a case where p and q (array name and pointer to the array)
> can not be used interchangably? I knew there is, such as sizeof( ).


So you already know that p is /not/ a pointer.

> p.s. I remembered reading somewhere on the Net says that the statement
> char *q = "Hello"
> is not a good style of programming. coz you do not know whether q
> points to a valid address or not.
> Is it true?


Not necessarily. You left out a lot of context.

Often
char *q = "Hello";
is better style than
char p[] = "Hello";
when you want the string to not be modifiable (initially).

But, because neither variable is declared as non-modifiable,
p can be overwritten with a string of 0 to 5 characters + the EOS,
and
q can be overwritten with a pointer to something (either NULL or
a real object)

HTH
- --
Lew Pitcher

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFFIUZ9agVFX4UWr64RAuMJAKDjlafFWoEsc2+YyZzrTj xYEKz4GACfULBM
+TThgTgF4qKJVzH9lGcNxoQ=
=Qd3q
-----END PGP SIGNATURE-----


Scott Fluhrer 10-02-2006 05:04 PM

Re: [pointers and arrays]: The difference between an array name and a pointer
 

<iamchiaweilin@gmail.com> wrote in message
news:1159806072.366409.117400@i3g2000cwc.googlegro ups.com...
> Hello all:
>
> What's the difference between p and q in the following statements?
>
>
> char p[] = "Hello";

Here's a paraphrase of what this means: "Mr. Compiler Sir: Please create an
array of 6 char's, which I will refer to as 'p'. Also, make its initial
contents the chars 'H', 'e', 'l', 'l', 'o', 0. Oh, and I might just write
new values in the array, if I feel like it"

> char *q = "Hello";

Here's a paraphrase of what this means: "Mr. Compiler Sir: Please create a
pointer to char, which I will refer to as 'q'. Also, make its initial
contents point to the first char of an unnamed array of 6 chars with the
initial contents 'H', 'e', 'l', 'l', 'o', 0. I promise not to write into
the unnamed array, but I might just point q somewhere else".

>
> I know q stores the address of 'H'.
> Question is: does p store the address of 'H' too?

No; p is an array, not a pointer.

> I know p is the name of the array that contains "Hello". Is array name
> a pointer?

No, it is not. In many contexts, an array name is automatically converted
into a pointer to the first element, but not all.

> In other words, is p exactly the same as &p[0]?

No, not exactly. For example:
sizeof(p) == 6
sizeof(&p[0]) == sizeof (char *)

>
> p and q are the same if you want to print them out by %s.
> Is there a case where p and q (array name and pointer to the array)
> can not be used interchangably? I knew there is, such as sizeof( ).

Here's two others:
p = "Bye!"; /* Error! */
q = "Bye!"; /* Works great! */
And:
p[0] = 'J'; /* Yummm! */
q[0] = 'J'; /* Undefined behavior (unless you've set q to point to a
writable char) */

>
> p.s. I remembered reading somewhere on the Net says that the statement
> char *q = "Hello"
> is not a good style of programming. coz you do not know whether q
> points to a valid address or not.
> Is it true?

Nonsense. Look at the paraphrase of the definition of q I gave above --
you told the compiler to point q at a valid memory location (the first char
of the unnamed array).

>




Richard Heathfield 10-02-2006 05:24 PM

Re: [pointers and arrays]: The difference between an array name and a pointer
 
iamchiaweilin@gmail.com said:

> Hello all:
>
> What's the difference between p and q in the following statements?
>
>
> char p[] = "Hello";
> char *q = "Hello";


p is an aggregate object, six bytes in size. sizeof p will confirm this. The
bytes contain 'H', 'e', 'l', 'l', 'o', and the null terminator character
'\0', in that order.

q is a pointer, its type being "pointer to char". Its value refers to the
place in memory where the string literal "Hello" (a six-character array
with static storage duration) is located.

> I know q stores the address of 'H'.


That's a rather informal way of putting it. :-) Specifically, it stores the
address of the particular 'H' that the compiler inserts somewhere in memory
and directly follows with 'e', 'l', 'l', 'o', and the null terminator
character '\0'.

> Question is: does p store the address of 'H' too?


No.

> I know p is the name of the array that contains "Hello". Is array name
> a pointer?


No, but it is treated like one most of the time. Exceptions: sizeof p and &p

> In other words, is p exactly the same as &p[0]?


In a value context, yes. Consider that p[i] and *(p + i) mean the same
thing, recognise that & and * are complementary (and therefore cancel), and
set i to 0:

p[i] means the same as *(p + i)
&p[i] means the same as &*(p + i)
&p[i] means the same as (p + i)
&p[0] means the same as (p + 0)
&p[0] means the same as (p )
&p[0] means the same as p


> p and q are the same if you want to print them out by %s.


If you mean printf will produce the same output for either when you pass
them as a match for the %s format specifier, you're right.

> Is there a case where p and q (array name and pointer to the array)
> can not be used interchangably? I knew there is, such as sizeof( ).


It's sizeof, not sizeof(). Yes, the other case is &

> p.s. I remembered reading somewhere on the Net says that the statement
> char *q = "Hello"
> is not a good style of programming.


Right. Better: const char *q = "Hello";

> coz you do not know whether q points to a valid address or not.
> Is it true?


No, the reason is that you mustn't modify the string literal. The const
helps the compiler to remind you not to do that.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Richard Heathfield 10-02-2006 05:26 PM

Re: [pointers and arrays]: The difference between an array name and a pointer
 
Scott Fluhrer said:

>
> <iamchiaweilin@gmail.com> wrote in message
> news:1159806072.366409.117400@i3g2000cwc.googlegro ups.com...
>> Hello all:
>>
>> What's the difference between p and q in the following statements?
>>
>>
>> char p[] = "Hello";

> Here's a paraphrase of what this means: "Mr. Compiler Sir: Please create
> an
> array of 6 char's, which I will refer to as 'p'. Also, make its initial
> contents the chars 'H', 'e', 'l', 'l', 'o', 0. Oh, and I might just write
> new values in the array, if I feel like it"


SCOTT! Ltnc! Welcome back.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)

Scott Fluhrer 10-02-2006 07:23 PM

Re: [pointers and arrays]: The difference between an array name and a pointer
 

"Richard Heathfield" <invalid@invalid.invalid> wrote in message
news:OeGdnQ1fxtEB1rzYnZ2dnUVZ8s-dnZ2d@bt.com...
> Scott Fluhrer said:
>
>>
>> <iamchiaweilin@gmail.com> wrote in message
>> news:1159806072.366409.117400@i3g2000cwc.googlegro ups.com...
>>> Hello all:
>>>
>>> What's the difference between p and q in the following statements?
>>>
>>>
>>> char p[] = "Hello";

>> Here's a paraphrase of what this means: "Mr. Compiler Sir: Please create
>> an
>> array of 6 char's, which I will refer to as 'p'. Also, make its initial
>> contents the chars 'H', 'e', 'l', 'l', 'o', 0. Oh, and I might just
>> write
>> new values in the array, if I feel like it"

>
> SCOTT! Ltnc! Welcome back.


Thanks. I've been busy with other things, but I'll be lurking from time to
time.

--
poncho



Keith Thompson 10-02-2006 11:03 PM

Re: [pointers and arrays]: The difference between an array name anda pointer
 
iamchiaweilin@gmail.com writes:
> Hello all:
>
> What's the difference between p and q in the following statements?
>
>
> char p[] = "Hello";
> char *q = "Hello";


Have you read section 6 of the comp.lang.c FAQ <http://www.c-faq.com/>?

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <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.


All times are GMT. The time now is 04:36 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.