how to pass symbollically the name of a struc member to a function ?

This is a discussion on how to pass symbollically the name of a struc member to a function ? within the C Programming forums, part of the General Programming Boards category; Hi all, Sorry in advance if this question has already been raised. I have a struct with several members, for ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    2

    Question how to pass symbollically the name of a struc member to a function ?

    Hi all,

    Sorry in advance if this question has already been raised.

    I have a struct with several members, for instance:

    insert
    Code:
    typedef struct {
    float u;
    float v;
    float w;
    } mystruct;
    
    main ()
    
    mystruct  test;
    
    ...
    
    I have a piece of code that should operate on test.u that looks like this:
    
    switch (j) {
    
    case 0:
     f = test.u + ...;
     break;
    
    case 1:
     f  = X - test.u;
     break;
    
    case 2:
     f = test.u + 2;
     break;
    
    }


    Now I would like to use a similar piece of code but for the other members, test.v and test.w. In principle I could copy the piece of code above and just replace test.u by test.v or test.w. Since it is the same code for all members, I would like to avoid this (copying many times the same piece of code) and replace it by a call to function. The question is, how to pass the name of the struct member I am considering to the function ? How can I tell the function, operates on member .u or .v ?
    Would it be possible to have a generic piece of code

    insert
    Code:
    function (member y,...)
    
    switch (j) {
    
    case 0:
     f = test.y + ...;
     break;
    
    case 1:
     f  = X - test.y;
     break;
    
    case 2:
     f = test.y + 2;
     break;
    
    }


    where the function could be called with function(u) or function(w) and would replace automatically .y by .u or .w ?

    Thanks in advance.
    Xtof

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    why not pass the member by value?
    Code:
    function (float y,...)
     
    switch (j) {
     
    case 0:
     f = y + ...;
     break;
     
    case 1:
     f  = X - y;
     break;
     
    case 2:
     f = y + 2;
     break;
     
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    2
    Quote Originally Posted by vart View Post
    why not pass the member by value?
    Code:
    function (float y,...)
     
    switch (j) {
     
    case 0:
     f = y + ...;
     break;
     
    case 1:
     f  = X - y;
     break;
     
    case 2:
     f = y + 2;
     break;
     
    }
    I forgot to mention that f is also a struct and has the same members. Actually, it should be something like:

    Code:
    function (member y,...)
    
    
    switch (j) {
    
    
    case 0:
     f.y = test.y + ...;
     break;
    
    
    case 1:
     f.y  = X - test.y;
     break;
    
    
    case 2:
     f.y = test.y + 2;
     break;
    
    
    }
    That is, if I pass the member .u, f.u should be calculated, if I pass .v, f.v should be calculated, and so on. Maybe I could pass simply f.u by reference.

    Code:
    function(&f.u,test.u);
    function(&f.v,test.v);
    function(&f.w,test.w);
    Do you think it would work like that ?

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by Xtof View Post
    Code:
    function(&f.u,test.u);
    function(&f.v,test.v);
    function(&f.w,test.w);
    Do you think it would work like that ?
    yes.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-30-2012, 07:18 AM
  2. unable to free 1 of the struc member
    By kakarato in forum C Programming
    Replies: 8
    Last Post: 12-20-2008, 04:53 PM
  3. How to pass member functions into a function object...
    By TeenWolf in forum C++ Programming
    Replies: 3
    Last Post: 04-24-2007, 02:01 PM
  4. Replies: 10
    Last Post: 09-15-2004, 02:00 PM
  5. syntax to pass a member function pointer to another class?
    By reanimated in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2003, 05:24 PM

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