Pointing to struture members.....

This is a discussion on Pointing to struture members..... within the C++ Programming forums, part of the General Programming Boards category; Hey all, What I would like to do is set up a ptr to point to a specific member of ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    208

    Pointing to struture members.....

    Hey all,

    What I would like to do is set up a ptr to point to a specific member of an array of structures. Now this pointer will be passed to my draw function and can be changed by user input. Essentially we have something like as follows:
    Code:
    ptr -> struct.member
    
    draw (ptr)
    
    //after user input
    
    ptr -> struct.member2
    So my questions are:

    1. I know all the members it will be pointing too are double's thus can I declare ptr as a double?
    2. Do I just point to the first member of the array of structures during initalization?
    3. If so, how do I access the rest of the specific members, located under other index numbers (i.e. struct[3].member vs struct[4].member, accessed with the pointer)?

    Thanks, andy help would be greatly appreciated.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    If I follow you correctly....
    Code:
    struct blah
    {
       double d1;
       double d2;
    };
    
    void Func(double *d)
    {
       //do something
    }
    
    ...
    
    struct blah b[5];
    
    for(i=0; i < 5; i++)
    {
       Func(&b[i].d1);
       Func(&b[i].d2);
    }
    This should do what you're asking
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    208
    yes, that will work, however, my visualization program is realtime. When a user presses a specific key I want to change that pointer, and the events are handled in a different source then my draw code so it is not as easy as just using a switch statement. I need something like this....

    Code:
    int main
    {    
           begineScene();
               draw(ptr);
           endScene();
    }
    now in events.h, when a specific user event occurs I want something like this

    Code:
    switch(key){
    case 1: ptr=struct.member1
    case 2: ptr = struct.member2
    }
    Now keep in mind Struct is actually and array of structs.
    Last edited by kas2002; 08-11-2006 at 10:31 AM.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    another approach is void pointer

    Kuphryn

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    312
    If the OP knows that all members will be of the type double, then I don't see how a void pointer will help.

    To the OP : The fact that you are using an array or container or structs shouldn't be of any serious consequence, you can use
    Code:
    arr[x].member1
    arr[y].member2
    etc.

    I think it could take a little reorganising of your program so that the switch statement knows beforehand which object it is working with. Maybe put the switch statement into an accessor function for the struct/class, so that you are always operating on the 'this' object, and you could simply call, eg,
    Code:
    arr[x].GetMember(3);
    Last edited by Bench82; 08-11-2006 at 06:42 PM.

  6. #6
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    If you keep your objects in an array, you could do some sort of hacky stuff like this:
    Code:
    myStruct structArray[20];
    float * pMember;
    switch( key )
    {
      case 1: pMember = &structArray[0].f1; break;
      case 2: pMember = &structArray[0].f2; break;
    }
    for( int i = 0; i < 20; i++ )
    {
      draw( pMember );
      pMember += sizeof( myStruct );
    }
    That only works if you know the memory is contiguous. If it's not contiguous, instead of storing a pointer to the member, you can store a byte offset.
    Code:
    LinkedList<myStruct> structList( 20 ); //makes a linked list with 20 myStructs
    unsigned int memberByteOffset;
    switch( key )
    {
      // you can make macros that will calculate the offset of a member given the class/struct name
      // and the member name, but I'll let you figure that part out if you choose this solution
      case 1: memberOffset = 0; break;
      case 2: memberOffset  = 4; break;
    }
    // probably would have an iterator of sort for a list, but this makes for an easy example
    for( int i = 0; i < 20; i++ )
    {
      byte * p = (byte *)&structList->Get( i ); 
      draw( (   (float*)( p + memberByteOffset ) )
    }

    OR, you can use member pointers, which is safer because it's C++'s built in mechanism for doing the memberByteOffset trick.
    Code:
    myStruct structArray[20]; 
    float myStruct::* pMember;
    
    switch( key )
    {
      case 1: pMember = &myStruct::f1;  break;
      case 2: pMember = &myStruct::f2;  break;
    }
    
    for( int i = 0; i < 20; i++ )
    {
      draw( structArray[i]::*pMember );
    }

  7. #7
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    If I am understanding you correctly, you have an array of stuctures that are all identical? You want to have a pointer that can point to each member of the structure using a key to determine where it points.

    Code:
    myStruct StructArray[20];
    double *ptr=NULL;
    
    index=0;  //basically use a index along with the key to specify 
    
          switch(key)
         { 
           case 1:  ptr=&StructArray[index].member1;
               break;
           }
    Last edited by manofsteel972; 08-12-2006 at 05:47 AM.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  8. #8
    Registered User
    Join Date
    May 2002
    Posts
    208
    Thanks all

    I think bench's idea is exactly what I am looking for.
    Jeff Paddon
    Undergraduate Research Assistant
    Physics Department
    St. Francis Xavier University

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamicly adding data members to structs
    By ITAmember in forum C Programming
    Replies: 11
    Last Post: 06-01-2009, 04:26 PM
  2. Throw away members in qsort
    By xxxrugby in forum C Programming
    Replies: 4
    Last Post: 04-24-2005, 07:08 AM
  3. What members does struct udphdr(linux) have?
    By failure_to in forum C Programming
    Replies: 2
    Last Post: 05-27-2004, 02:07 PM
  4. VC++ IntelliSense (showing class members)
    By _Elixia_ in forum Tech Board
    Replies: 0
    Last Post: 08-02-2003, 02:14 PM

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