difference between a pointer array and a stack array?

Discussion in 'C Programming' started by Ahmad Humayun, May 25, 2008.

  1. Whats the difference between:

    char str1[] = "wxyz";
    char* str2 = "abcd";

    I can do this:
    str2 = str1

    but I can't do this:
    str1 = str2

    (isn't str1 technically a pointer?)


    Thanks
     
    Ahmad Humayun, May 25, 2008
    #1
    1. Advertising

  2. Ahmad Humayun <> wrote:
    > Whats the difference between:


    > char str1[] = "wxyz";
    > char* str2 = "abcd";


    > I can do this:
    > str2 = str1


    > but I can't do this:
    > str1 = str2


    > (isn't str1 technically a pointer?)


    No. 'str1' is an array of 5 chars, initialized to the characters
    'w', 'x', 'y', 'z' and '\0'. Only if 'str1' is used in a place
    where a value is required (e.g. if 'str1' is an argument of a
    function call) it gets replaced automatically by a pointer to
    the first element of that array. But that doesn't change any-
    thing about the "nonpointerness" of 'str1', it's an array and
    remains to be an array until it goes out of scope.

    In contrast, 'str2' is a pointer, initialized to point to the
    string literal "abcd" (that could very well be in read-only
    memory). Since 'str2' is a pointer you can assign it a different
    value, e.g. by using

    str2 = str1;

    This works because in this case on the right hand side of the
    asignment a value is required and now "the rule" applies, i.e.
    that if an array is used in a context where a value is needed
    it is replaced by a pointer to its first element.

    This automatic conversion is actually not much different from
    what happens when you write

    int a = 1.2;

    Since on the right hand side an int value is required the double
    value you have there is automatically converted to an int value.

    C could in principle refrain from doing such automatic conversions
    and require that you explicitely state your intent like

    int a = ( int ) 1.2;

    or

    str2 = &str[ 0 ];

    but that's not how the inventors of C decided to do it and in-
    stead introduced some automatic conversions.

    But

    str1 = str2;

    is still a syntax error since 'str1' is not a pointer and can't
    be treated like a pointer because it has a completely different
    type.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, May 25, 2008
    #2
    1. Advertising

  3. Ahmad Humayun <> writes:
    > Whats the difference between:
    >
    > char str1[] = "wxyz";
    > char* str2 = "abcd";


    str1 is an array; str2 is a pointer.

    > I can do this:
    > str2 = str1


    Right. str1, an array expression, is implicitly converted to a
    pointer in most contexts, including this one.

    > but I can't do this:
    > str1 = str2


    Right, you can't assign to an array.

    > (isn't str1 technically a pointer?)


    No, str1 is an array. Arrays are not pointers; pointers are not
    arrays. This is probably the most common misconception about C.

    Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>. Feel
    free to post again with more specific questions if you're still
    confused after that.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 25, 2008
    #3
  4. On May 26, 1:04 am, Keith Thompson <> wrote:
    > Ahmad Humayun <> writes:
    > > Whats the difference between:

    >
    > > char str1[] = "wxyz";
    > > char* str2 = "abcd";

    >
    > str1 is an array; str2 is a pointer.
    >
    > > I can do this:
    > > str2 = str1

    >
    > Right.  str1, an array expression, is implicitly converted to a
    > pointer in most contexts, including this one.
    >
    > > but I can't do this:
    > > str1 = str2

    >
    > Right, you can't assign to an array.
    >
    > > (isn't str1 technically a pointer?)

    >
    > No, str1 is an array.  Arrays are not pointers; pointers are not
    > arrays.  This is probably the most common misconception about C.
    >
    > Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.  Feel
    > free to post again with more specific questions if you're still
    > confused after that.
    >
    > --
    > Keith Thompson (The_Other_Keith)  <http://www.ghoti.net/~kst>
    > Nokia
    > "We must do something.  This is something.  Therefore, we must do this.."
    >     -- Antony Jay and Jonathan Lynn, "Yes Minister"


    Thanks Jens and Antony....this info was really really helpful :)

    Happy coding :)
     
    Ahmad Humayun, May 26, 2008
    #4
  5. On May 26, 1:04 am, Keith Thompson <> wrote:
    > Ahmad Humayun <> writes:
    > > Whats the difference between:

    >
    > > char str1[] = "wxyz";
    > > char* str2 = "abcd";

    >
    > str1 is an array; str2 is a pointer.
    >
    > > I can do this:
    > > str2 = str1

    >
    > Right.  str1, an array expression, is implicitly converted to a
    > pointer in most contexts, including this one.
    >
    > > but I can't do this:
    > > str1 = str2

    >
    > Right, you can't assign to an array.
    >
    > > (isn't str1 technically a pointer?)

    >
    > No, str1 is an array.  Arrays are not pointers; pointers are not
    > arrays.  This is probably the most common misconception about C.
    >
    > Read section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.  Feel
    > free to post again with more specific questions if you're still
    > confused after that.
    >
    > --
    > Keith Thompson (The_Other_Keith)  <http://www.ghoti.net/~kst>
    > Nokia
    > "We must do something.  This is something.  Therefore, we must do this.."
    >     -- Antony Jay and Jonathan Lynn, "Yes Minister"


    Thanks Jens and Antony....this info was really really helpful :)

    Happy coding :)
     
    Ahmad Humayun, May 26, 2008
    #5
  6. Ahmad Humayun

    Guest

    On May 25, 10:37 pm, Ahmad Humayun <> wrote:
    > Whats the difference between:
    >
    > char str1[] = "wxyz";
    > char* str2 = "abcd";
    >
    > I can do this:
    > str2 = str1
    >
    > but I can't do this:
    > str1 = str2
    >
    > (isn't str1 technically a pointer?)

    str1 would be a pointer only in function declarations and definitions:

    int foo(char str1[], char *str2) {
    str1 = str2; /* valid */
    return 0;
    }
     
    , May 27, 2008
    #6
  7. 4On Tue, 27 May 2008 06:20:12 -0700 (PDT), wrote:

    >On May 25, 10:37 pm, Ahmad Humayun <> wrote:
    >> Whats the difference between:
    >>
    >> char str1[] = "wxyz";
    >> char* str2 = "abcd";
    >>
    >> I can do this:
    >> str2 = str1
    >>
    >> but I can't do this:
    >> str1 = str2
    >>
    >> (isn't str1 technically a pointer?)


    No, str1 is technically an array.

    >str1 would be a pointer only in function declarations and definitions:


    str1 is never a pointer but it is converted to a pointer in many
    cases, not just the two you mention.

    When used in an expression, str1 has type array of 5 char. As such,
    this expression will be automatically converted by the compiler to a
    pointer to the first element of the array with type pointer to char
    (effectively &str1[0]) in every case except:
    when used as the operand of the sizeof operator
    when used as the operand of the & operator

    (There is a third exception but it applies only to string literals
    used to initialize an array as part of the array definition.)

    This is why the statement str2 = str1; is legal. str2 has type char*.
    The expression str1 is converted to an expression that has type char*.
    It is legal to assign an expression of type char* to an object of type
    char*.

    >
    >int foo(char str1[], char *str2) {
    > str1 = str2; /* valid */
    > return 0;
    >}



    Remove del for email
     
    Barry Schwarz, May 28, 2008
    #7
  8. Ahmad Humayun

    Guest

    On May 28, 4:58 am, Barry Schwarz <> wrote:
    > 4On Tue, 27 May 2008 06:20:12 -0700 (PDT), wrote:
    >
    > >On May 25, 10:37 pm, Ahmad Humayun <> wrote:
    > >> Whats the difference between:

    >
    > >> char str1[] = "wxyz";
    > >> char* str2 = "abcd";

    >
    > >> I can do this:
    > >> str2 = str1

    >
    > >> but I can't do this:
    > >> str1 = str2

    >
    > >> (isn't str1 technically a pointer?)

    >
    > No, str1 is technically an array.
    >
    > >str1 would be a pointer only in function declarations and definitions:

    >
    > str1 is never a pointer but it is converted to a pointer in many
    > cases, not just the two you mention.

    str1 in my example code is a pointer.
    > When used in an expression, str1 has type array of 5 char. As such,
    > this expression will be automatically converted by the compiler to a
    > pointer to the first element of the array with type pointer to char
    > (effectively &str1[0]) in every case except:
    > when used as the operand of the sizeof operator
    > when used as the operand of the & operator
    >
    > (There is a third exception but it applies only to string literals
    > used to initialize an array as part of the array definition.)
    >
    > This is why the statement str2 = str1; is legal. str2 has type char*.
    > The expression str1 is converted to an expression that has type char*.

    No, str1 is a char * (in my example).
    > It is legal to assign an expression of type char* to an object of type
    > char*.
    >
    >
    >
    > >int foo(char str1[], char *str2) {
    > > str1 = str2; /* valid */
    > > return 0;
    > >}


    See question 6.4 of the c-faq.
    <http://c-faq.com/>
     
    , May 28, 2008
    #8
  9. writes:
    > On May 28, 4:58 am, Barry Schwarz <> wrote:
    >> 4On Tue, 27 May 2008 06:20:12 -0700 (PDT), wrote:
    >>
    >> >On May 25, 10:37 pm, Ahmad Humayun <> wrote:
    >> >> Whats the difference between:

    >>
    >> >> char str1[] = "wxyz";
    >> >> char* str2 = "abcd";

    >>
    >> >> I can do this:
    >> >> str2 = str1

    >>
    >> >> but I can't do this:
    >> >> str1 = str2

    >>
    >> >> (isn't str1 technically a pointer?)

    >>
    >> No, str1 is technically an array.
    >>
    >> >str1 would be a pointer only in function declarations and definitions:

    >>
    >> str1 is never a pointer but it is converted to a pointer in many
    >> cases, not just the two you mention.

    > str1 in my example code is a pointer.


    str1, in the only example code quoted here, is an array, not a
    pointer.

    [...]

    >> This is why the statement str2 = str1; is legal. str2 has type char*.
    >> The expression str1 is converted to an expression that has type char*.

    > No, str1 is a char * (in my example).


    Then perhaps your example got lost. The sample code, if the
    attributions are correct was originally posted by Ahmad Humayun; in
    that code, str1 is declared as an array.

    [snip]

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 28, 2008
    #9
  10. Ahmad Humayun

    Guest

    On May 28, 8:11 pm, Keith Thompson <> wrote:
    > writes:
    > > On May 28, 4:58 am, Barry Schwarz <> wrote:
    > >> 4On Tue, 27 May 2008 06:20:12 -0700 (PDT), wrote:

    >
    > >> >On May 25, 10:37 pm, Ahmad Humayun <> wrote:
    > >> >> Whats the difference between:

    >
    > >> >> char str1[] = "wxyz";
    > >> >> char* str2 = "abcd";

    >
    > >> >> I can do this:
    > >> >> str2 = str1

    >
    > >> >> but I can't do this:
    > >> >> str1 = str2

    >
    > >> >> (isn't str1 technically a pointer?)

    >
    > >> No, str1 is technically an array.

    >
    > >> >str1 would be a pointer only in function declarations and definitions:

    >
    > >> str1 is never a pointer but it is converted to a pointer in many
    > >> cases, not just the two you mention.

    > > str1 in my example code is a pointer.

    >
    > str1, in the only example code quoted here, is an array, not a
    > pointer.
    >
    > [...]
    >
    > >> This is why the statement str2 = str1; is legal. str2 has type char*.
    > >> The expression str1 is converted to an expression that has type char*.

    > > No, str1 is a char * (in my example).

    >
    > Then perhaps your example got lost. The sample code, if the
    > attributions are correct was originally posted by Ahmad Humayun; in
    > that code, str1 is declared as an array.
    >
    > [snip]

    My example was right in that [snip].
    Let's follow the discussion, first my message:

    -- message --
    On May 25, 10:37 pm, Ahmad Humayun <> wrote:
    > Whats the difference between:


    > char str1[] = "wxyz";
    > char* str2 = "abcd";


    > I can do this:
    > str2 = str1


    > but I can't do this:
    > str1 = str2


    > (isn't str1 technically a pointer?)


    str1 would be a pointer only in function declarations and definitions:

    int foo(char str1[], char *str2) { <------ my example
    str1 = str2; /* valid */
    return 0;

    }
    -- message --

    Then Mr Schwarz reply:

    -- message --
    4On Tue, 27 May 2008 06:20:12 -0700 (PDT), wrote:

    >On May 25, 10:37 pm, Ahmad Humayun <> wrote:
    >> Whats the difference between:


    >> char str1[] = "wxyz";
    >> char* str2 = "abcd";


    >> I can do this:
    >> str2 = str1


    >> but I can't do this:
    >> str1 = str2


    >> (isn't str1 technically a pointer?)


    No, str1 is technically an array.

    >str1 would be a pointer only in function declarations and definitions:


    str1 is never a pointer but it is converted to a pointer in many
    cases, not just the two you mention.

    When used in an expression, str1 has type array of 5 char. As such,
    this expression will be automatically converted by the compiler to a
    pointer to the first element of the array with type pointer to char
    (effectively &str1[0]) in every case except:
    when used as the operand of the sizeof operator
    when used as the operand of the & operator

    (There is a third exception but it applies only to string literals
    used to initialize an array as part of the array definition.)

    This is why the statement str2 = str1; is legal. str2 has type char*.
    The expression str1 is converted to an expression that has type char*.
    It is legal to assign an expression of type char* to an object of type
    char*.

    >int foo(char str1[], char *str2) { /* <---- my code here again */
    > str1 = str2; /* valid */
    > return 0;
    >}


    Remove del for email
    -- message --

    I think it's clear that when I said this:
    > str1 would be a pointer only in function declarations and definitions:
    > <snip code>

    I was talking about my code, and not Mr Humayuns code.
    Then Mr Schwarz says:

    > >str1 would be a pointer only in function declarations and definitions:

    > str1 is never a pointer but it is converted to a pointer in many
    > cases, not just the two you mention.

    Mr Schwarz either took that out of context or he was not aware that in
    my example, indeed, str1 is a pointer.
     
    , May 28, 2008
    #10
  11. writes:
    > On May 28, 8:11 pm, Keith Thompson <> wrote:

    [...]
    >> str1, in the only example code quoted here, is an array, not a
    >> pointer.
    >>
    >> [...]
    >>
    >> >> This is why the statement str2 = str1; is legal. str2 has type char*.
    >> >> The expression str1 is converted to an expression that has type char*.
    >> > No, str1 is a char * (in my example).

    >>
    >> Then perhaps your example got lost. The sample code, if the
    >> attributions are correct was originally posted by Ahmad Humayun; in
    >> that code, str1 is declared as an array.
    >>
    >> [snip]

    > My example was right in that [snip].

    [...]

    So it was.

    The article to which I replied did have a declaration of str1 (as an
    array), and I missed the other declaration that appeared at the bottom
    of that article, after your statement that you had declared it as a
    pointer.

    Incidentally, the original declaration:

    char str1[] = "wxyz";

    cannot be a pointer declaration; for it to be one, you'd have to move
    it into a function prototype *and* drop the `` = "wxyz";''.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, May 28, 2008
    #11
    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,390
  2. Replies:
    7
    Views:
    435
    Keith Thompson
    Oct 3, 2006
  3. Zach
    Replies:
    9
    Views:
    697
    Thad Smith
    Feb 20, 2007
  4. chris
    Replies:
    6
    Views:
    1,007
    chris
    Oct 28, 2005
  5. somenath
    Replies:
    10
    Views:
    293
    James Kanze
    Jul 2, 2013
Loading...

Share This Page