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




<iamchiaweilin@xxxxxxxxx> wrote in message
news:1159806072.366409.117400@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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).




.



Relevant Pages

  • Re: Difference between Char* ptr and char arrCh []
    ... I have a few queries regarding array of characters using array ... notation and pointer notation. ... Is there a difference in storage of global char* and char* inside ...
    (comp.lang.c)
  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: Returning pointer to array problem II
    ... Iam trying to make program were I enter string and serach char. ... and funktion prints out witch position char is found this is done if funktion serach_char. ... so far all good what I want do next is: return, from funktion, pointer value to array were positions is stored. ...
    (comp.lang.c)
  • Re: Simple question on Pointers
    ... int main ... It stores 12 char and only 12 ... pointer to the first element of the array with type pointer to element ...
    (microsoft.public.vc.language)
  • Re: Window Management
    ... (* prompt is a string resource identifier specifying the string ... PROCEDURE YesNoCancel(prompt: ARRAY OF CHAR; ... VAR INOUT response: ARRAY OF CHAR): BOOLEAN; ...
    (comp.lang.ada)