1

I am still learning about pointers and structs, but I hoping someone might know if it is possible to access individual members sequentially by use of a pointer?

    Typedef record_data {  
     float a;  
     float b;  
     float c;  
     }records,*Sptr;  

    records lists[5];  
    Sptr ptr;   

Example: assign all members of the 5 lists with a value of float 1.0

    // instead of this      
    (void)testworks(void){     
    int i;
    float j=1.0;  
    ptr = &lists[i]  
    ptr->lists[0].a = j;  
    ptr->lists[0].b = j;  
    ptr->lists[0].c = j;  
    ptr->lists[1].a = j;  
    // ... and so on  
    ptr->lists[4].c = j;  
    }  


    // want to do this
    (void)testwannado(void){  
    int a,i;
    float j=1.0;  
    ptr = &lists[i]   
    for (a=0;a<5;a++){  // step through typedef structs   
        for (i=0;i<3;i++){  // step through members   
        ???    
        }  
    }  

Forgive my errors in this example below, but it represents the closest thing I can think of for want I am trying to accomplish.

    int *mptr;  
    mptr = &(ptr->lists[0].a)   // want to assign a pointer to members so all 3 members can be used...  
    *mptr++ = j;  // so I can do something like this.

This wasn't compiled, so any other errors are unintentional.

  • correct `float *mptr;` is your error I mean you defined for int. – Grijesh Chauhan Dec 05 '12 at 18:03
  • yes, originally when I wrote this example I made the members int for simplicity, but considering the length of float might possibly have significance for addressing in sequenctial stepping I changed it last minute. – user1879874 Dec 05 '12 at 18:07
  • 1
    useful or worked? what present status? – Grijesh Chauhan Dec 05 '12 at 18:09
  • sorry, I was thinking something else. The assignment of *mptr++ = j; this is the end goal, where "mptr" is a pointer to ptr->lists[0].a (which I know is not correct, but used to give you the idea of what I am trying to do) i.e. struct[].member = 1.0; That being said, I have found nothing in my past several hours of searching anything which indicates that I can move down my list of members. Moving by structs works fine of course. Oddly enough, I can do this mptr = (mptr + 4) and it then is set to point at the next member (brushing aside the aweful etiquette) but I can not assign to it. – user1879874 Dec 05 '12 at 19:29

1 Answers1

0

You generally don't want to do that. Structure members should be accessed individually. You can run into a lot of portability problems by assuming the memory layout of how multiple consecutive structure members are placed in memory. And most (C-like) languages do not give you a way to "introspect" through the members of a structure.

Bogatyr
  • 19,255
  • 7
  • 59
  • 72
  • Is it definite that `struct` members are not consecutive address? I think he is doing correct! the problem is type of `*mptr`. – Grijesh Chauhan Dec 05 '12 at 18:06
  • It is definite that it is not *guaranteed* that they are consecutive addresses. Some platforms/compilers may have them consecutive, some may not. For fields of the same type, most platforms may in fact place them consecutively. But you should not write software that depends on an undefined aspect of the language. Different platforms are allowed to pack and align fields as they please. Again, I'm assuming we're talking about C-like languages. – Bogatyr Dec 05 '12 at 21:22