Solved

# return Multiple value

Posted on 2011-09-13
338 Views
How can we return multiple values using pointers
0
Question by:nagaharikola
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 2
• 2
• +2

LVL 7

Expert Comment

ID: 36527843

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

LVL 40

Expert Comment

ID: 36528110
Depends what you mean by "multiple values". Some clarification of what you are trying to achieve would help.
0

Author Comment

ID: 36529108
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

Author Comment

ID: 36529112
please give code snippet . i am a beginner
0

LVL 53

Accepted Solution

Infinity08 earned 250 total points
ID: 36529169
>> 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

Author Comment

ID: 36529221
yes
0

LVL 53

Expert Comment

ID: 36529268
0

Author Comment

ID: 36529277
with out pointer to a structure can we return multiple values.
0

LVL 9

Expert Comment

ID: 36529580
>>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

LVL 9

Expert Comment

ID: 36529643
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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn hoā¦
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and infā¦
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
###### Suggested Courses
Course of the Month5 days, 10 hours left to enroll