in Programming in C retagged by
998 views
0 votes
0 votes

The following program runs perfectly fine without showing compilation error. I am unable to understand why as 'c' is a local variable of 'function_addition'. So, shouldn't it throw an error?

#include<stdio.h>

int function_addition(int a, int b)
{
int c;
c = 10;
return c;
}
int main()
{ //function_addition(13, 15);
printf("%d", function_addition(13,15) );
}

Also, another similar program involving strings(see below) throws an error when we try to print it in a similar way.

#include<stdio.h>
char *getstring(){
char str[]="GateOverflow";
return str;
}
int main()
{
printf("%s", getstring());
}// Gives error

Please explain me this ambiguity as to why the first one prints the value of c correctly whereas the second program shows an error.

in Programming in C retagged by
998 views

2 Answers

3 votes
3 votes
Best answer
Here the difference is the first program returning value not address.In the first program, you are just returning the value of C and not the address it is printing the value.

In the second program, you are returning address of str which is vanished once you come out of the function.So it is giving an exception when trying to print it.

PS: Accessing a local variable outside the scope is undefined behaviour -- we may not always get a segmentation fault here.
edited by
1 vote
1 vote
                #include<stdio.h>
                #include<string.h>
                char *getstring(){
                char *str="GateOverflow";// here the program is giving error . Array value cannot be return through pointer
                return str;
                }
                int main()
                {
                printf("%s", getstring());
                }

In 1st program c is a local variable and After  returning c value in main() , it simply prints the value.

But in 2nd program ,it takes the value in an array. And returning it in main() with a pointer. As address of array is constant, returning through pointer giving the error

13 Comments

@Srestha  Your program will work fine since you've used char *str="GateOverflow"; and not  char str[]="GateOverflow";.

*str will be stored in shared segment and will not be destroyed even after the activation record of *getstring() vanishes.

Also, we can return the address of an array using a pointer since arrays and pointers work in a similar manner.
0
0
Then according to ur assumption in 1st program c also should vanish after activation record destroyed. Then how will it print 10?
0
0
That's what my doubt was.

What I understood is that return c returns the "value" of the variable whereas return str returns the "address" which, in the case of char str[], doesn't exist( it gets destroyed when the activation record gets destroyed.)

If we try to return something like return(&c), the program won't execute.
0
0
What do I think is ,  in program 1) c is printing the value in the same line, where function is called. So, activation record is still not deleted.
In program 2) return str or even *str will not give right result. I think only cause here is array is a static  memory allocation.
U can try with any array , and can try to print with address or with pointer. U will not get right result.
rt?
0
0
@Srestha You should not assume such things. We can, of course, return an array address as a pointer -- any reason why it can't?

The problem here is that the array is local to the current function stack and the stack space is not valid outside the function. Also, returning the array -- same as returning its base address -- only returns the base address and not copies the array contents. When we return a local int variable, its value gets copied-- not when we return an array in C. In some other languages, this does happen.
1
1
@Arjun Sir

means call by reference in both program? But how do we assume first program is using call by reference?

Otherwise c is local variable. It shouldnot get value from main()
0
0
C has only call by value.
0
0
So, while c variable returning in 1st program, c variable value should be deleted when coming out from stack. Then why r we getting 10?
0
0
The return value gets "Copied back". In a function, the return value is returned similar to arguments being passed to it. Usually, a specific register is used for this -- and that is why C allows only 1 value to be returned.
1
1
but
https://gateoverflow.in/3466/gate2007-it-33
here program is not taking other function value other than global variable.
So, why in this program it will take return value of other function?
0
0

https://gateoverflow.in/3466/gate2007-it-33

In this program value also copied , but not back to main() function.

So, why in this given program value is copied back.

Can u plz point out, where my fault is?

0
0
That is NOT C language and pass by reference is given in the question.
1
1

Actually I tried with some varity in this program.Still not getting satisfactory result. Each time there is a variety in output. If we  " return an array address as a pointer " in a function to a pointer , it will give runtime error

https://ideone.com/kIKZOc

0
0

Related questions

4 votes
4 votes
2 answers
3
Dexter asked in Compiler Design Apr 12, 2016
2,102 views
Dexter asked in Compiler Design Apr 12, 2016
by Dexter
2.1k views