Thread: Change struct members inside a function and pass to another functions

  1. #1
    Registered User
    Join Date
    Jul 2019
    Posts
    11

    Change struct members inside a function and pass to another functions

    Hello All


    Could not understand usage of pointers to pointers as a function input. Basically I want to fill struct members inside a function and pass to another functions as filled with pointers.



    Code:
    struct application {
    
    int a_onehit=0
    
    };
    
    struct application *fill_struct_application() 
    
    int main(){
        struct application *appointer= fill_struct_application();
        
        printf("main: %f\n", appointer->a_onehit);
    
    return 0;
    }
    
    struct application *fill_struct_application() {
    
    
        
        static struct application ap,*pptr;
        pptr = (struct application*)malloc(sizeof(struct application));
        
        pptr = ≈
        
        RT_APPLICATION_INIT(pptr); //not important for now
        pptr->a_onehit = 1;
        
        
        
    
    
        
        
        return pptr;
    
    
    }
    Tried with various approaches including pointers to pointers but always "a_onehit" showed "0" like "ap" and "pptr" can only live inside "fill_struct_application" function. So how can pass "ap" as filled inside "fill_struct_application" to another functions as filled.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,237
    It looks like the simplest solution for you would be:
    Code:
    struct application {
        int a_onehit = 1;
    };
    If you prefer, write the default constructor:
    Code:
    struct application {
        application() : a_onehit(1) {}
    
        int a_onehit;
    };
    If you really, really want to use a separate non-member function, then make use of a reference parameter:
    Code:
    struct application {
        int a_onehit;
    };
    
    void fill_struct_application(application& x) {
        x.a_onehit = 1;
    }
    but I don't recommend this as it is essentially unnecessary two-stage object construction. Do it for other use cases, i.e., setting the member variable to a replacement value.

    Generally, you should not use malloc in C++ unless you're writing an allocator or need to do some special case stuff.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-22-2019, 01:50 AM
  2. Change string inside the function
    By totoco in forum C Programming
    Replies: 1
    Last Post: 01-02-2018, 10:51 AM
  3. Replies: 2
    Last Post: 03-26-2012, 10:10 PM
  4. Replies: 17
    Last Post: 07-06-2011, 11:44 AM
  5. accessing members inside of a private struct
    By MyglyMP2 in forum C++ Programming
    Replies: 9
    Last Post: 04-26-2007, 10:06 PM

Tags for this Thread