By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,187 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

Oct 2 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
ia***********@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.
Oct 2 '06 #2

P: n/a

<ia***********@gmail.comwrote in message
news:11**********************@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).
>

Oct 2 '06 #3

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
ia***********@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-----

Oct 2 '06 #4

P: n/a
ia***********@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)
Oct 2 '06 #5

P: n/a
Scott Fluhrer said:
>
<ia***********@gmail.comwrote in message
news:11**********************@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)
Oct 2 '06 #6

P: n/a

"Richard Heathfield" <in*****@invalid.invalidwrote in message
news:Oe******************************@bt.com...
Scott Fluhrer said:
>>
<ia***********@gmail.comwrote in message
news:11**********************@i3g2000cwc.googlegr oups.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
Oct 2 '06 #7

P: n/a
ia***********@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) ks***@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.
Oct 2 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.