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



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


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

.



Relevant Pages

  • Re: Structures with variable length array known at compile time
    ... the array "menu_items" will always have 20 character strings but the ... You have the array the wrong what round -- what you wrote is an array ... of 20 arrays of q characters each. ... One way is to have a pointer to an array. ...
    (comp.lang.c)
  • Re: Bug/Gross InEfficiency in HeathFields fgetline program
    ... It could legally be a pointer to a character array of at least n characters, ... If so, the strlencall will not terminate at the end of the array, so this code would have undefined behavior. ... There's no guarantee in the C standard that it goes into segfault. ...
    (comp.lang.c)
  • Re: fgets not doing as I expect.
    ... I wanted to limit how many chars I read in, ... number of characters specified by n from the stream pointed ... immediately after the last character read into the array. ... null pointer is returned. ...
    (comp.lang.c)
  • Re: chars
    ... A string is a pointer to an array of ... A string is, by definition, "a contiguous sequence of characters ... terminated by and including the first null character", and a "pointer ...
    (comp.lang.c)
  • Re: atomics: document that linux expects certain atomic behaviour from unsigned long
    ... pointer loads and stores. ... compiler reloading pointers/longs/ints to access local variables... ...
    (Linux-Kernel)