Thread: Advice on how to call a struct function inside another struct function

  1. #1
    Registered User
    Join Date
    Oct 2018
    Posts
    14

    Advice on how to call a struct function inside another struct function

    Hi, I have a struct function (struct 1) and I want to call another struct function (struct 2) from struct 1 using input of struct 1 itself. Basically :

    • Inside Struct Age, I need to calculate Age. And using conditional statement, if Age requirement is accepted --> Call struct Housing.
    • Struct Housing using input from 2 different structs, including struct Age (the caller struct). So I define struct Housing before struct Age.

    Code is below :
    This is the 1st struct
    Code:
    struct Salary salary (struct Hour hours)
    {  //some calculation
    
    
        struct Salary result;
        result.S = S;
        return result;
    }
    This is the 2nd struct
    Code:
    struct Housing house (struct Salary sal, struct Age member)
    {  //calculation & calling value sal.S (from struct Salary) 
         //and member.age (from struct Age)
    
    
        struct Housing result;
        result.D = D;
        return result;
    }

    And this is where I need to call struct inside conditional statement :
    Code:
    struct Age pass (struct Hour hours, int x, int y)
    {   //some calculation
        
        if (fabs (a2-a1) < 5){
            printf("Age requirement - %3.f %3.f : PASS \n",x,y);
            struct Age result;
            result.age  = age;
            result.ret  = TRUE;
            return result;
    
            struct Housing H = house(salary, pass); //Error here
            //This struct call its own func (pass). 
        }
        
        //some false statements
    }

    However I encountered error :
    Code:
    Error : error: passing 'struct Salary (struct Hours)' to parameter of incompatible type 'struct Salary'
    struct Housing H = house(salary, pass);
                             ^~~~~
    passing argument to parameter 'sal' here
    I hope my explanation isn't confusing. Where did I miss in my code ?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    You cannot "call" struct objects. Rather, you call functions. In this case, you want to call a function and pass a struct object as an argument:
    Code:
    struct Housing H = house(salary(hours), result);
    But you have a return statement before this, so this means that this will never be executed.

    Also, it might help if you started naming your functions with verbs instead of nouns, since you seem confused. For example, instead of "house", come up with a descriptive name involving a verb as to what that function does, e.g., computeHousingSomething, where Something is replaced by sonething only you know. Also, stop using names like S and D. Use descriptive names. I would give suggestions, but it is precisely because you failed to be descriptive that I don't know what these are about. Sally and Donovan? Sugar and Donut?
    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

  3. #3
    Registered User
    Join Date
    Oct 2018
    Posts
    14
    Quote Originally Posted by laserlight View Post
    You cannot "call" struct objects. Rather, you call functions. In this case, you want to call a function and pass a struct object as an argument:
    Code:
    struct Housing H = house(salary(hours), result);
    But you have a return statement before this, so this means that this will never be executed.

    Also, it might help if you started naming your functions with verbs instead of nouns, since you seem confused. For example, instead of "house", come up with a descriptive name involving a verb as to what that function does, e.g., computeHousingSomething, where Something is replaced by sonething only you know. Also, stop using names like S and D. Use descriptive names. I would give suggestions, but it is precisely because you failed to be descriptive that I don't know what these are about. Sally and Donovan? Sugar and Donut?
    Thank you for your suggestion and advice on naming variable, I'll work on it. Also, if I don't return the value first, how am I supposed to use it as an input for a struct inside the same condition. Maybe I should change the function after all.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    Quote Originally Posted by pixie_laluna
    if I don't return the value first, how am I supposed to use it as an input for a struct inside the same condition.
    I think you meant "function" or "scope", not "condition". Anyway, if you return a value from the function, control leaves the function and re-enters the function that called that function. If you want to use a struct object, after assigning to it, just use it.
    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

  5. #5
    Registered User
    Join Date
    Oct 2018
    Posts
    14
    Quote Originally Posted by laserlight View Post
    I think you meant "function" or "scope", not "condition". Anyway, if you return a value from the function, control leaves the function and re-enters the function that called that function. If you want to use a struct object, after assigning to it, just use it.
    Thank for correction ! English isn't my main language. We usually call function / scope with other name in our language so I wasn't aware. Also, the function that call a function with return value (struct Age) here is my main function. But I can't call the Housing struct in the main as well, because this function doesn't need to be executed all the time. Only if Age returns true.

    Maybe I can return the return value of Age and make statement condition in main to check if it's true then I can call Housing. If possible I want to do it inside Age, but as for now I try to check which one works.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,248
    This is an example of an if statement, as in the whole code is an if statement:
    Code:
    if (condition)
    {
        // body of if statement
        // ...
    }
    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

  7. #7
    Registered User
    Join Date
    Oct 2018
    Posts
    14
    Quote Originally Posted by laserlight View Post
    This is an example of an if statement, as in the whole code is an if statement:
    Code:
    if (condition)
    {
        // body of if statement
        // ...
    }
    You're too kind. Even I understand the structure of if condition. Thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to call a function that takes a STRUCT parameter
    By fouzimedfouni in forum C Programming
    Replies: 2
    Last Post: 02-24-2015, 10:24 AM
  2. Returning a struct from a function call
    By hzcodec in forum C Programming
    Replies: 4
    Last Post: 07-09-2012, 01:50 PM
  3. Replies: 17
    Last Post: 07-06-2011, 11:44 AM
  4. struct in function call
    By Jasper in forum C Programming
    Replies: 2
    Last Post: 09-09-2009, 02:18 AM
  5. Replies: 2
    Last Post: 05-04-2003, 05:30 AM

Tags for this Thread