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

Discussion in 'C Programming' started by iamchiaweilin@gmail.com, Oct 2, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. pemo Guest

    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.
    pemo, Oct 2, 2006
    #2
    1. Advertising

  3. Lew Pitcher Guest

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


    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-----
    Lew Pitcher, Oct 2, 2006
    #3
  4. <> wrote in message
    news:...
    > 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).

    >
    Scott Fluhrer, Oct 2, 2006
    #4
  5. 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.

    --
    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, Oct 2, 2006
    #5
  6. Scott Fluhrer said:

    >
    > <> wrote in message
    > news:...
    >> 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)
    Richard Heathfield, Oct 2, 2006
    #6
  7. "Richard Heathfield" <> wrote in message
    news:...
    > Scott Fluhrer said:
    >
    >>
    >> <> wrote in message
    >> news:...
    >>> 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
    Scott Fluhrer, Oct 2, 2006
    #7
  8. Re: [pointers and arrays]: The difference between an array name anda pointer

    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) <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.
    Keith Thompson, Oct 3, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jakk
    Replies:
    4
    Views:
    12,052
  2. Me
    Replies:
    79
    Views:
    5,252
    Dan Pop
    Jun 18, 2004
  3. Ahmad Humayun
    Replies:
    10
    Views:
    541
    Keith Thompson
    May 28, 2008
  4. shashi
    Replies:
    17
    Views:
    403
    Guest
    Apr 13, 2006
  5. somenath
    Replies:
    10
    Views:
    265
    James Kanze
    Jul 2, 2013
Loading...

Share This Page