Multi-Dimensional variable in struct in C program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • apmsbask
    New Member
    • May 2010
    • 2

    Multi-Dimensional variable in struct in C program

    I am stuck with how to access a 2-d array declared in a structure. For instance, I have declared a struct as follows:

    Code:
    struct sv {
    float a[10][10];
    };
    Now in main function, how to access the variable 'a'? I tried the following

    Code:
    main () {
    struct sv * p;
    p = (struct sv *) malloc (sizeof(struct sv));
    *(p->*(a+1)+1) = 20.0;
    printf("%f\n",*(p->*(a+1)+1));
    }
    But, this program gives the following error:
    error: expected identifier before ‘*’ token
    Last edited by Niheel; May 22 '10, 03:36 AM. Reason: codetags
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Why don't you use

    p->a[1][1] = 20.0;

    Comment

    • apmsbask
      New Member
      • May 2010
      • 2

      #3
      Dear Banfa

      Thanks for your reply. Your suggestion works well.

      I also found an alternate way to do it. In fact somewhere in the web, I read that when declaring a struct, one has to know the array size apriori. But this is not true. For instance, the following program works
      Code:
      //---------------------------------------------------------------------------------------
      #include <stdio.h>
      #include <stdlib.h>
      
      struct sv {
      float **a;
      };
      
      main () {
         int i;
         struct sv *p;
         p = (struct sv *) malloc (sizeof(struct sv)*2);
         p->a = (float **) malloc (sizeof(float)*10);
         (p+1)->a = (float **) malloc (sizeof(float)*10);
         for(i=0;i<10;i++)
        {
            *(p->a+i) = (float *) malloc (sizeof(float)*10);
            *((p+1)->a+i) = (float *) malloc (sizeof(float)*10);
            *(*((p+0)->a+i)+2) = i;
            p->a[i][i]=i; //Suggested by Banfa 
            printf("%f %f %f\n",*(*((p+1)->a+i)+2),*(*(p->a+i)+2),p->a[i][i]);
         }
      }
      //-----------------------------------------------------------------------------------------
      The output will be

      0.000000 0.000000 0.000000
      0.000000 1.000000 1.000000
      0.000000 2.000000 2.000000
      0.000000 3.000000 3.000000
      0.000000 4.000000 4.000000
      0.000000 5.000000 5.000000
      0.000000 6.000000 6.000000
      0.000000 7.000000 7.000000
      0.000000 8.000000 8.000000
      0.000000 9.000000 9.000000

      I spent one full day in this. I felt that I should share this experience with others so that they need not spent time at all. When I was stuck I found that there is no single book which talks about multi-dim array inside a structure. Also no online notes or manuals discuss about this. If anybody knows a good book or website which discusses multi-dim arrays inside a struct, kindly let me know.

      Thanks in advance.

      Baskar
      Last edited by Niheel; May 22 '10, 03:36 AM. Reason: codetags

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        While that is true you have not in fact declared any arrays in your last example only pointers which is why it would have been unlikely to come up during a search for arrays. When declaring an array you do need to know its size. There are some significant differences between arrays and pointers.

        Arrays inside a structure work exactly the same as arrays outside a structure which is why you have not found the covered separately.

        And finally you may want to try reading this which talks about multi-dimensional arrays a lot.

        Comment

        Working...