pointer to array of structure

This is a discussion on pointer to array of structure within the C Programming forums, part of the General Programming Boards category; Hello, I am not sure why I am not able to print member structure member aa. Can someone please help? ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    10

    Post pointer to array of structure

    Hello, I am not sure why I am not able to print member structure member aa. Can someone please help? Code and output below:

    Code:
    #include <ansi_c.h>
    // Structuer passing example
    
    typedef struct
    {
        char t[10];
        int w, h, a;
        char *aa;
    } area; // area is declared to be of type struct
    
    
    void assign(void); // prototypes 
    area table[20]; // declare an array of 20 area's
    area (*p)[20] = &table; // declare and define pointer-to-array of 20 area's
    
    void assign()  // use it
    {
        char str[10],str2[10];
        
        for(int i=0; i<20; i++)
        {
            sprintf(str,"T%d\0",i);
            strcpy((*p)[i].t,str);
            (*p)[i].w = i; // dereference p to get to table, 
                                // [i] to access i-th structure and 
                                // .w to get the w member of it 
            (*p)[i].h = i;
            (*p)[i].a = (*p)[i].w*(*p)[i].h;
            sprintf(str2,"A%d\0",i);
            (*p)[i].aa = str2;
            printf("aa[%d]=%s\n",i,(*p)[i].aa);
        }
    }
    
    int main(void)
    {
        assign();   // initial table
        
        if(&p != NULL)
        {                                        
            printf("No.  TT W * H = Area  AA\n========================\n");
            for(int j=0; j<20; j++)
            {
                printf("[%2d] %-3s %2d %2d   %3d %3s\n",j, (*p)[j].t,(*p)[j].w,(*p)[j].h,(*p)[j].a,(*p)[j].aa);  // why doest this work aa (prints garbage)?
            }
        }
        else
            printf("pTable (= NULL) not pointing to anything (i.e. has not been intiaialized\n");
                
        getchar();
        
        return 0;
    }
    result printed:
    aa[0]=A0
    aa[1]=A1
    aa[2]=A2
    aa[3]=A3
    aa[4]=A4
    aa[5]=A5
    aa[6]=A6
    aa[7]=A7
    aa[8]=A8
    aa[9]=A9
    aa[10]=A10
    aa[11]=A11
    aa[12]=A12
    aa[13]=A13
    aa[14]=A14
    aa[15]=A15
    aa[16]=A16
    aa[17]=A17
    aa[18]=A18
    aa[19]=A19
    No. TT W * H = Area AA
    ========================
    [ 0] T0 0 0 0 H?hO
    XhH?h?
    [ 1] T1 1 1 1 H?hO
    XhH?h?
    [ 2] T2 2 2 4 H?hO
    XhH?h?
    [ 3] T3 3 3 9 H?hO
    XhH?h?
    [ 4] T4 4 4 16 H?hO
    XhH?h?
    [ 5] T5 5 5 25 H?hO
    XhH?h?
    [ 6] T6 6 6 36 H?hO
    XhH?h?
    [ 7] T7 7 7 49 H?hO
    XhH?h?
    [ 8] T8 8 8 64 H?hO
    XhH?h?
    [ 9] T9 9 9 81 H?hO
    XhH?h?
    [10] T10 10 10 100 H?hO
    XhH?h?
    [11] T11 11 11 121 H?hO
    XhH?h?
    [12] T12 12 12 144 H?hO
    XhH?h?
    [13] T13 13 13 169 H?hO
    XhH?h?
    [14] T14 14 14 196 H?hO
    XhH?h?
    [15] T15 15 15 225 H?hO
    XhH?h?
    [16] T16 16 16 256 H?hO
    XhH?h?
    [17] T17 17 17 289 H?hO
    XhH?h?
    [18] T18 18 18 324 H?hO
    XhH?h?
    [19] T19 19 19 361 H?hO
    XhH?h?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    > (*p)[i].aa = str2;
    Because this does NOT make a copy of the string.
    All you end up with is 20 pointers, all pointing at str2 - well until the function ends, at which point str2 goes out of scope and you have 20 pointers all pointing at garbage.

    The simple fix, until you know what you're doing is
    Code:
    typedef struct
    {
        char t[10];
        int w, h, a;
        char aa[20];
    } area; // area is declared to be of type struct
    followed by
    Code:
    strcpy((*p)[i].aa,str2);
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    10
    Salem, thx for your response.
    So are you saying I cannot use a char pointer as a member inside a structure (i.e. it would have to be a string...like char name[10] instead of char* name)?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,505
    You can use a char pointer, but you need to call malloc() to allocate the right amount of space for the string (then copy it), and then later on make sure you call free() when you're done.

    Once you start managing memory yourself, it's a significant step up in the difficulty in writing code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. write to file .. pointer to structure array
    By mad_muppet in forum C Programming
    Replies: 11
    Last Post: 05-07-2011, 05:33 AM
  2. structure array pointer function help
    By BrandNew in forum C Programming
    Replies: 4
    Last Post: 03-07-2011, 09:48 PM
  3. Structure array inside memory pointer
    By muggly in forum C Programming
    Replies: 8
    Last Post: 12-15-2009, 02:15 PM
  4. Replies: 6
    Last Post: 01-16-2007, 08:21 PM
  5. pointer to an array in a structure
    By samual_van in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2005, 10:51 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21