use of "->" on arrays?

Discussion in 'C Programming' started by Paminu, Oct 11, 2005.

  1. Paminu

    Paminu Guest

    I thought that "->" was only used with pointers. Why is it that it is also
    allowed to use it on arrays??

    struct test {
    int a;
    int b;
    };

    struct test array[5];

    void wierd()
    {
    array->b = 200;
    printf("array->b: %d\n", array->b);

    }

    int main()
    {
    wierd();
    return 1;
    }


    It is not allowed if I do something like:

    array[1]->b = 200;

    so how do I know which element that gets field b set to 200 when I type:

    array->b = 200;
    Paminu, Oct 11, 2005
    #1
    1. Advertising

  2. In article <dihal1$thj$-c.dk>, Paminu <> wrote:

    >struct test {
    > int a;
    > int b;
    >};
    >
    >struct test array[5];
    >
    >void wierd()
    >{
    > array->b = 200;
    > printf("array->b: %d\n", array->b);
    >
    >}


    A nice example... In most contexts, an array name is converted to a
    pointer to its first element, so array->b is equivalent to (*array).b
    which is equivalent to array[0].b.

    -- Richard
    Richard Tobin, Oct 11, 2005
    #2
    1. Advertising

  3. "Paminu" <> wrote in message
    news:dihal1$thj$-c.dk...
    > I thought that "->" was only used with pointers. Why is it that it is also
    > allowed to use it on arrays??
    >
    > struct test {
    > int a;
    > int b;
    > };
    >
    > struct test array[5];
    >
    > void wierd()
    > {
    > array->b = 200;
    > printf("array->b: %d\n", array->b);
    >
    > }
    >
    > int main()
    > {
    > wierd();
    > return 1;
    > }
    >
    >
    > It is not allowed if I do something like:
    >
    > array[1]->b = 200;
    >
    > so how do I know which element that gets field b set to 200 when I type:
    >
    > array->b = 200;


    hologram>cat my_array.c
    #include <stdio.h>

    struct test
    {
    int a;
    int b;
    }; /* struct test */

    struct test array[5];

    void wierd(void) /* BTW, itz spelled "weird" */
    {
    int i;

    array->b = 200;
    printf("array->b: %d\narray[0].b: %d\n\n", array->b, array[0].b);
    for(i=0; i<5; i++)
    {
    (array+i)->a = i;
    (array+i)->b = i * i;
    } /* for i */
    } /* wierd */

    int main(void)
    {
    int i;

    wierd();
    for(i=0; i<5; i++)
    {
    printf("(array+%d)->a: %d; (array+%d)->b: %d\n", i, (array+i)->a, i,
    (array+i)->b);
    printf("array[%d].a: %d; array[%d].b: %d\n\n", i, array.a, i,
    array.b);
    } /* for i */

    return 0;
    } /* main */
    hologram>gcc -Wall -ansi -pedantic -o my_array.exe my_array.c
    hologram>my_array.exe
    array->b: 200
    array[0].b: 200

    (array+0)->a: 0; (array+0)->b: 0
    array[0].a: 0; array[0].b: 0

    (array+1)->a: 1; (array+1)->b: 1
    array[1].a: 1; array[1].b: 1

    (array+2)->a: 2; (array+2)->b: 4
    array[2].a: 2; array[2].b: 4

    (array+3)->a: 3; (array+3)->b: 9
    array[3].a: 3; array[3].b: 9

    (array+4)->a: 4; (array+4)->b: 16
    array[4].a: 4; array[4].b: 16

    Bottom line: there are a couple of different ways of coding the same thing
    due to the *similar* properties of arrays and pointers in C. However you
    will probably want to refer to section 6 of the C FAQ
    (http://www.faqs.org/faqs/C-faq/faq/) to learn what the similarities (and
    most important, the differences) really are.

    -Charles
    Charles M. Reinke, Oct 11, 2005
    #3
  4. On 2005-10-11 18:26:05 -0400, "Charles M. Reinke"
    <> said:

    > void wierd(void) /* BTW, itz spelled "weird" */


    [OT]
    And "itz" is spelled "it's" ;-)
    [/OT]

    --
    Clark S. Cox, III
    Clark S. Cox III, Oct 12, 2005
    #4
  5. Paminu

    Guest

    Paminu wrote:
    > I thought that "->" was only used with pointers. Why is it that it is also
    > allowed to use it on arrays??
    >
    > struct test {
    > int a;
    > int b;
    > };
    >
    > struct test array[5];
    >
    > void wierd()
    > {
    > array->b = 200;
    > printf("array->b: %d\n", array->b);
    >
    > }
    >
    > int main()
    > {
    > wierd();
    > return 1;
    > }
    >
    >
    > It is not allowed if I do something like:
    >
    > array[1]->b = 200;
    >
    > so how do I know which element that gets field b set to 200 when I type:
    >
    > array->b = 200;


    You are correct that "->" is only used with pointers. In your exampale
    "array" is nothing but a pointer. It's the base address of the array.
    In other words, address of array[0].
    Following is not allowed as the operator -> expects a pointer.
    array[1]->b = 200;
    But you can always right (array+1)->b = 200; Here array+1 is a pointer
    to second element of the array.

    Regards,
    Raju
    , Oct 12, 2005
    #5
  6. writes:
    > Paminu wrote:
    >> I thought that "->" was only used with pointers. Why is it that it is also
    >> allowed to use it on arrays??

    [...]
    >> struct test array[5];

    [...]
    > You are correct that "->" is only used with pointers. In your
    > exampale "array" is nothing but a pointer. It's the base address of
    > the array. In other words, address of array[0].


    That's both right and wrong. "array" itself is an array object, not a
    pointer. The name "array", when used in an expression, is converted
    to a pointer value.

    > Following is not allowed as the operator -> expects a pointer.
    > array[1]->b = 200;
    > But you can always right (array+1)->b = 200; Here array+1 is a pointer
    > to second element of the array.


    Yes -- but of course it's much less clear than "array[1].b = 200;".

    --
    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 12, 2005
    #6
    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. Alexandra Stehman
    Replies:
    5
    Views:
    30,604
    Chris Smith
    Jun 17, 2004
  2. Bill Reyn
    Replies:
    3
    Views:
    2,253
    Bob Hairgrove
    Jun 22, 2004
  3. Mantorok Redgormor

    initializing arrays of arrays

    Mantorok Redgormor, Sep 10, 2003, in forum: C Programming
    Replies:
    4
    Views:
    565
  4. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    385
  5. Philipp
    Replies:
    21
    Views:
    1,127
    Philipp
    Jan 20, 2009
Loading...

Share This Page