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
How can I open files mentioned on the command line, and parse option flags?
write a program to find the given number is prime or not
Write a program to print factorial of given number using recursion?
What is data type long in c?
What is c variable?
How are variables declared in c?
What does %2f mean in c?
What are directives in c?
What is the main difference between calloc () and malloc ()?
Why do we use int main instead of void main in c?
Which is better pointer or array?
What are the data types present in c?
Do you know the difference between malloc() and calloc() function?
stripos — Find position of first occurrence of a case- insensitive string int stripos ( char* haystack, char* needle, int offset ) Returns the numeric position of the first occurrence of needle in the haystack string. Note that the needle may be a string of one or more characters. If needle is not found, stripos() will return -1. The function should not make use of any C library function calls.
How to compare array with pointer in c?