• C

return Multiple value

How can we return multiple values using pointers
nagaharikolaAsked:
Who is Participating?
 
Infinity08Commented:
>> i know about returning structures but i don't know how to return multiple values with pointers

Can you return a pointer to a struct ?
0
 
HalfAsleepCommented:

Integer pointers are declared as int * value.

You will have to construct a function which takes your normal parameters, as well as return pointer parameters as input.

As you calculate your return values inside the function, you assign the value to the pointers from the function parameters to return them to the caller.  The caller must allocate their data and pass pointers into your function in addition to any normal parameters needed.

Forgive me for not spelling it out, but the question sounds a bit academic in nature.
0
 
evilrixSenior Software Engineer (Avast)Commented:
Depends what you mean by "multiple values". Some clarification of what you are trying to achieve would help.
0
Increase Security & Decrease Risk with NSPM Tools

Analyst firm, Enterprise Management Associates (EMA) reveals significant benefits to enterprises when using Network Security Policy Management (NSPM) solutions, while organizations without, experienced issues including non standard security policies and failed cloud migrations

 
nagaharikolaAuthor Commented:
how to return multiple values from a function
as u know we can do this with  structures and pointers.
i know about returning structures but i don't know how to return multiple values with pointers
0
 
nagaharikolaAuthor Commented:
please give code snippet . i am a beginner
0
 
nagaharikolaAuthor Commented:
yes
0
 
Infinity08Commented:
So, that's your answer then :)
0
 
nagaharikolaAuthor Commented:
with out pointer to a structure can we return multiple values.
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
>>with out pointer to a structure can we return multiple values.
Yes. But you need to pass those many pointers as per your requirements as parameters to the function.
But it's better to use pointer to structure by which you need to pass only one(or less no. of) pointers to structure.
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
suppose you need a function to return sum,difference,multiplication and division of two numbers, then
your function prototype will look as follows.

void sum_diff_mul_div(int a, int b, int* sum, int* diff, int* mul, int* div);

or
struct result{
int sum,
int diff,
int mul,
int div};

void sum_diff_mul_div(int a, int b, struct result* res);

Now you decide between two versions which one is better?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.