Solved

return Multiple value

Posted on 2011-09-13
11
329 Views
Last Modified: 2012-05-12
How can we return multiple values using pointers
0
Comment
Question by:nagaharikola
  • 4
  • 2
  • 2
  • +2
11 Comments
 
LVL 7

Expert Comment

by:HalfAsleep
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

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

Author Comment

by:nagaharikola
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

by:nagaharikola
ID: 36529112
please give code snippet . i am a beginner
0
 
LVL 53

Accepted Solution

by:
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

by:nagaharikola
ID: 36529221
yes
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 36529268
So, that's your answer then :)
0
 

Author Comment

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

Expert Comment

by:Subrat (C++ windows/Linux)
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 8

Expert Comment

by:Subrat (C++ windows/Linux)
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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Retrun object in plist format 5 55
undefined reference to `bswap_128' 9 137
Which checksum is this? 7 139
C hashtable library 3 83
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 opening and reading files in the C programming language.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now