Thread: Help using memcpy to update structure members

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    15

    Help using memcpy to update structure members

    I am trying to update the members of a structure using memcpy and running into issues determining the indexes of the elements. here is an example

    Code:
    struct AA{
      double a;
      double b;
      double c;
     }
    
    
    
    struct BB{
      double d;
      int       e;
      double f;
      int       g;
    }
    
    double U[3]
    double V[2]
    int W[2]
    (void) memcpy(&AA (double*)U,, sizeof(double*)U);

    This works fine. However I am not sure how to update struct BB since it has mixed data types. I would like to update structure BB with the values of V and W. I want to use memcpy since the element declaration of these structures can change and I don't have access to them. Can someone help me how to access the location of each structure member.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Why wouldn't you just use the = sign?

    AA.a = u[x];

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I want to use memcpy since the element declaration of these structures can change and I don't have access to them. Can someone help me how to access the location of each structure member.
    Can you elaborate more?
    If it's opaque type, then better write setter/getter function?

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    Hello Bayint,
    I am writing a C program to load a library(dll) and update certain values in the dll. I don’t have the source code of the dll. However the dll provides me the Structure, number of members and types of each member in the structure. I need to update these members with new values. Since I don’t know the member names AA.a=u[x] doesn’t work for me. The only way I can think of accomplishing this is by updating the addresses. So if all the member types are of the same type then “(void) memcpy(&AA (double*)U, sizeof(double*)U);” would work. However I am not sure how to accomplish this if the member types are mixed.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Be lazy:
    Code:
    union
    {
        struct thingineedtoupdate foo;
        struct whateverithinkiamdoing bar;
    } baz;
    
    baz.bar = doingstuff;
    dowhateverithinkiamupdating( baz.foo );

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    I am sorry, I don't understand this. Is there a way to access the address of structure members so that I can use memcpy to update them.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sure, if you know what the members are.
    Code:
    &somestruct.somemember
    I don't understand why you think you need to use memcpy if you know what every one of the members are. Just assign values directly, as you've already been told.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    15
    The problem is I don't know the members. All I know are the number of members and their data types and the datatypes are mixed within the structure.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well I'm still not sure what you're stuck on. How can you be updating something if you don't know what it is?
    Code:
    struct foo
    {
        type thingyouknow;
        type thingyoudonotknow;
        type otherthingyouknow;
    };
    How can you expect to accurately update 'thingyoudonotknow' if you don't know what it is or what its type is? What are you trying to put there ... and why, if you don't know anything about what it is? None of that makes sense.

    Answer that first, and you should be able to do whatever you need with everything else here we've already given you.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help
    By fourseventwo in forum C Programming
    Replies: 4
    Last Post: 12-04-2010, 11:01 PM
  2. Accessing Structure Members
    By anndruu12 in forum C Programming
    Replies: 5
    Last Post: 12-02-2010, 02:37 AM
  3. Replies: 5
    Last Post: 11-16-2010, 12:22 PM
  4. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  5. Replies: 14
    Last Post: 06-28-2006, 01:58 AM

Tags for this Thread