How to return multiple values from a function?
Answers were Sorted based on User's Feedback
Answer / r. kumaran
Make array of values and return the array address as long.
Now using pointer traverse through the value.
Is This Answer Correct ? | 61 Yes | 19 No |
Answer / prasanta maiti
Make array of values and return the array address as long.
simple example:
void main()
{
int a[10],i,n;
int *new_value_array;
printf("\n Enter how many number you want to enter? ");
scanf("%d",&n);
for(i=0;i<n;i++)
sacnf("%d",&a[i]);
printf("\n old value of the array is:");
for(i=0;i<n;i++)
printf("\t%d",a[i]);
new_value_array = array_modify(a,n);
printf("\n new value of the array is: \n");
for(i=0;i<n;i++)
printf("\t%d",*(new_value_array+i));
getch();
}
int *array_modify(int a[10],int n)
{
int i;
for(i=0;i<n;i++)
a[i]=a[i]*4;
return(a);
}
Is This Answer Correct ? | 45 Yes | 14 No |
Answer / shruti
Multiple values can be returned from a function,
using the call by refrance method.
in this method, we pass pointers as the argument to the
funtion..
Is This Answer Correct ? | 38 Yes | 15 No |
Answer / saurabh
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct
{
int a;
int b;
}Mystruct;
Mystruct myfun();
int main()
{
Mystruct ms2;
ms2 = myfun();
printf("val1: %d val2: %d",ms2.a,ms2.b);
return 0;
}
Mystruct myfun()
{
int a,b;
Mystruct ms;
a = 10;
b = 20;
ms.a=a;
ms.b=b;
return(ms);
}
Is This Answer Correct ? | 14 Yes | 15 No |
Answer / manjunath sudnye
Just Make Array Of Pointer For Any Data Type And Return The
Base Address Of Thgat Pointer.
e.g
int *Ptr[5]={1,2,3,4,5};
//now see you can use 5 pointers for manupulation the
valuesand can return it by.
return Ptr;
Is This Answer Correct ? | 2 Yes | 5 No |
Answer / j.nithya
we can return multiple values from a function by using pointer.... Just store your multiple values which you want to return and return the base address......Store the values in the array and return the base address of the array.
Is This Answer Correct ? | 3 Yes | 10 No |
Answer / splurgeop
you can return multiple values from the function by using
mechanism called "call-by-reference".
for example:
void main()
{
int area,*circumference;
area=function(area,circumference);
cout<<"the area is "<<area;
cout<<"the circumference is"<<circumference;
}
int function(int a,int *b)
{
a=12;
int temp=a*3;
b=&temp;
return a;
}
// this is just an example............
// you can also use array and return the adress of the
array in the main and can use to traverse the entire array
and return multiple values from function.
Is This Answer Correct ? | 34 Yes | 60 No |
main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); }
Printf can be implemented by using __________ list.
main() { int i=0; for(;i++;printf("%d",i)) ; printf("%d",i); }
struct aaa{ struct aaa *prev; int i; struct aaa *next; }; main() { struct aaa abc,def,ghi,jkl; int x=100; abc.i=0;abc.prev=&jkl; abc.next=&def; def.i=1;def.prev=&abc;def.next=&ghi; ghi.i=2;ghi.prev=&def; ghi.next=&jkl; jkl.i=3;jkl.prev=&ghi;jkl.next=&abc; x=abc.next->next->prev->next->i; printf("%d",x); }
Can you send Code for Run Length Encoding Of BMP Image in C Language in linux(i.e Compression and Decompression) ?
What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?
main() { int i=3; switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; } }
write a c-program to find gcd using recursive functions
#include <stdio.h> #define a 10 main() { #define a 50 printf("%d",a); }
Given an array of size N in which every number is between 1 and N, determine if there are any duplicates in it. You are allowed to destroy the array if you like.
21 Answers ABC, eBay, Goldman Sachs, Google, HUP, Microsoft, TATA,
prog. to produce 1 2 3 4 5 6 7 8 9 10
# include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); }