dynamic array of struct

Discussion in 'C Programming' started by Excluded_Middle, Mar 22, 2005.

  1. what is wrong with this code

    #include <stdio.h>

    typedef struct s1
    {
    int a;
    int b;
    }s1;

    int main()
    {
    s1 *tmp;

    tmp = (s1 *)calloc(10,sizeof(s1));
    if(tmp == NULL) /* Error checking done */

    tmp[5]->a = 90;
    tmp[5]->b = 239;

    printf("a is %d\n",tmp[5]->a);
    printf("b is %d\n",tmp[5]->b);
    free(tmp);
    return 0;
    }

    in this program i want to create a dynamic array of s1. and then i
    want to access each element using array notation like tmp[5] for the
    6th element in the array.

    thats what i get when I compile it using cc comand on linux OS
    buffer.c: In function `main':
    buffer.c:16: invalid type argument of `->'
    buffer.c:17: invalid type argument of `->'
    buffer.c:19: invalid type argument of `->'
    buffer.c:20: invalid type argument of `->'
     
    Excluded_Middle, Mar 22, 2005
    #1
    1. Advertising

  2. Excluded_Middle

    Eric Sosman Guest

    Excluded_Middle wrote:
    > what is wrong with this code
    >
    > #include <stdio.h>
    >
    > typedef struct s1
    > {
    > int a;
    > int b;
    > }s1;
    >
    > int main()
    > {
    > s1 *tmp;
    >
    > tmp = (s1 *)calloc(10,sizeof(s1));
    > if(tmp == NULL) /* Error checking done */
    >
    > tmp[5]->a = 90;


    tmp[5] is a struct, not a pointer to a struct.
    Use `.' instead of `->'.

    --
     
    Eric Sosman, Mar 22, 2005
    #2
    1. Advertising

  3. Excluded_Middle

    Artie Gold Guest

    Excluded_Middle wrote:
    > what is wrong with this code
    >
    > #include <stdio.h>
    >
    > typedef struct s1
    > {
    > int a;
    > int b;
    > }s1;
    >
    > int main()
    > {
    > s1 *tmp;
    >
    > tmp = (s1 *)calloc(10,sizeof(s1));
    > if(tmp == NULL) /* Error checking done */
    >
    > tmp[5]->a = 90;

    `tmp[5]' is an object of type `struct s1', as opposed to being a pointer
    to one, so
    tmp[5].a = 90;
    is what you want.
    > tmp[5]->b = 239;

    Similarly.
    >
    > printf("a is %d\n",tmp[5]->a);
    > printf("b is %d\n",tmp[5]->b);
    > free(tmp);
    > return 0;
    > }
    >
    > in this program i want to create a dynamic array of s1. and then i
    > want to access each element using array notation like tmp[5] for the
    > 6th element in the array.
    >
    > thats what i get when I compile it using cc comand on linux OS
    > buffer.c: In function `main':
    > buffer.c:16: invalid type argument of `->'
    > buffer.c:17: invalid type argument of `->'
    > buffer.c:19: invalid type argument of `->'
    > buffer.c:20: invalid type argument of `->'

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://it-matters.blogspot.com (new post 12/5)
    http://www.cafepress.com/goldsays
     
    Artie Gold, Mar 22, 2005
    #3
  4. Excluded_Middle wrote:
    > what is wrong with this code


    Compare your code

    > #include <stdio.h>
    > typedef struct s1
    > {
    > int a;
    > int b;
    > }s1;


    > int main()
    > {
    > s1 *tmp;
    > tmp = (s1 *)calloc(10,sizeof(s1));
    > if(tmp == NULL) /* Error checking done */
    > tmp[5]->a = 90;
    > tmp[5]->b = 239;
    > printf("a is %d\n",tmp[5]->a);
    > printf("b is %d\n",tmp[5]->b);
    > free(tmp);
    > return 0;
    > }


    with
    #include <stdio.h>
    #include <stdlib.h>

    typedef struct s1
    {
    int a;
    int b;
    } s1;

    int main()
    {
    s1 *tmp;

    if (!(tmp = malloc(10 * sizeof *tmp))) {
    fprintf(stderr, "malloc failed.\n" "bailing ...\n");
    exit(EXIT_FAILURE);
    }
    tmp[5].a = 90;
    tmp[5].b = 239;
    printf("a is %d\n", tmp[5].a);
    printf("b is %d\n", tmp[5].b);
    free(tmp);
    return 0;
    }
     
    Martin Ambuhl, Mar 22, 2005
    #4
  5. Excluded_Middle

    CBFalconer Guest

    Excluded_Middle wrote:
    >
    > what is wrong with this code


    You put a cast where it doesn't belong and suppressed an error
    message, and other things.

    >
    > #include <stdio.h>
    >
    > typedef struct s1
    > {
    > int a;
    > int b;
    > }s1;
    >
    > int main()
    > {
    > s1 *tmp;
    >
    > tmp = (s1 *)calloc(10,sizeof(s1));

    ^^^^^^
    get rid of this cast and you will get an error message here. A
    better statement would be:

    tmp = malloc(10 * sizeof *tmp);

    and initialize things properly yourself if needed. calloc
    guarantees all bytes zero, but that does not properly initialize
    such fields as reals and pointer. The error message should lead
    you to "#include <stdlib.h>" at some point.

    > if(tmp == NULL) /* Error checking done */
    >
    > tmp[5]->a = 90;


    the type of tmp[5] is s1. s1 is not a pointer. It has fields a
    and b, which you access with dot notation, i.e. "tmp[5].a = 90;".
    You could also use "(tmp + 5)->a". And so forth.

    > tmp[5]->b = 239;
    >
    > printf("a is %d\n",tmp[5]->a);
    > printf("b is %d\n",tmp[5]->b);
    > free(tmp);
    > return 0;
    > }
    >
    > in this program i want to create a dynamic array of s1. and then i
    > want to access each element using array notation like tmp[5] for the
    > 6th element in the array.
    >
    > thats what i get when I compile it using cc comand on linux OS
    > buffer.c: In function `main':
    > buffer.c:16: invalid type argument of `->'
    > buffer.c:17: invalid type argument of `->'
    > buffer.c:19: invalid type argument of `->'
    > buffer.c:20: invalid type argument of `->'



    --
    "I conclude that there are two ways of constructing a software
    design: One way is to make it so simple that there are obviously
    no deficiencies and the other way is to make it so complicated
    that there are no obvious deficiencies." -- C. A. R. Hoare
     
    CBFalconer, Mar 23, 2005
    #5
    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. Chris Fogelklou
    Replies:
    36
    Views:
    1,391
    Chris Fogelklou
    Apr 20, 2004
  2. Daniel Rudy
    Replies:
    7
    Views:
    455
    Daniel Rudy
    Mar 31, 2006
  3. Replies:
    3
    Views:
    1,099
  4. TiloVillwock

    struct array within struct

    TiloVillwock, May 15, 2010, in forum: C Programming
    Replies:
    11
    Views:
    713
    Nick Keighley
    May 16, 2010
  5. Tuan  Bui
    Replies:
    14
    Views:
    477
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page