Can u return two values using return keyword? If yes, how?
If no, why?
Answer Posted / vikas shakya
Using the return statement u can only return one value at a
time.
So you can either return the value of a variable like you
can return an integer, or you can return pointer (which may
contain more than one values), which is pointing to
dynamically allocated location, Like in given below example:
//Returning two values from a function.
#include "stdio.h"
#include "malloc.h"
int *values()
{
int *ptr;
ptr = (int*)malloc(2);
*ptr = 10;
*(ptr+1) = 20;
return ptr;
}
int main()
{
int *ptr = values();
printf("%d\n%d",*ptr,*(ptr+1));
return 0;
}
| Is This Answer Correct ? | 2 Yes | 3 No |
Post New Answer View All Answers
What is indirection? How many levels of pointers can you have?
Is c a great language, or what?
What is the difference between text and binary i/o?
Are the outer parentheses in return statements really optional?
What are the differences between Structures and Arrays?
Explain the difference between getch() and getche() in c?
Can an array be an Ivalue?
What are the different properties of variable number of arguments?
Who is the main contributor in designing the c language after dennis ritchie?
What is const keyword in c?
How does selection sort work in c?
The number of bytes of storage occupied by short, int and long are a) 2, 2 and 4 b) 2, 4 and 4 c) 4, 4 and 4 d) none
Explain how does flowchart help in writing a program?
How can variables be characterized?
What are the types of pointers?