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

I

iamchiaweilin

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?
 
P

pemo

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

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


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+YyZzrTjxYEKz4GACfULBM
+TThgTgF4qKJVzH9lGcNxoQ=
=Qd3q
-----END PGP SIGNATURE-----
 
S

Scott Fluhrer

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

Richard Heathfield

(e-mail address removed) 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 and *(p + i) mean the same
thing, recognise that & and * are complementary (and therefore cancel), and
set i to 0:

p means the same as *(p + i)
&p means the same as &*(p + i)
&p 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.
 
R

Richard Heathfield

Scott Fluhrer said:
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.
 
S

Scott Fluhrer

Richard Heathfield said:
Scott Fluhrer said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top