+ 1

Pointers in C

How does this code produce the correct output? I thought the function variables are destroyed after the function's execution https://code.sololearn.com/cCBrMtQoB87Y/?ref=app

31st May 2021, 8:47 AM
Rishi
Rishi - avatar
9 Réponses
+ 1
Rishi you are just returning a value from the function. First inside the function (square()) : You declare "result" of type "int” and place an integral value in it. After that you tried to return that INTEGER value to your calling function which expects to receive a memory address so your compiler tells it to treat that value as a memory address and warns you about the same. The second type conversion ( from void* to int ) happens by the assignment operator on line 9 ( compiler warns you about this also ) and now compiler tells your program to treat the same value as an integer value and not a memory address, thus you get the required result.
31st May 2021, 5:00 PM
Arsenic
Arsenic - avatar
+ 2
Yes, the function variables are destroyed as the stack rolls back after the function execution, but here you are returning value of variable "result" ( or more appropriately a copy of it ) and not the variable itself. And even your compier is telling you about the implicit conversions it is performing to you via warnings :- 1) returning 'int' from a function with return type 'void *' makes pointer from integer without a cast 2) assignment to 'int' from 'void * makes integer from pointer without a cast
31st May 2021, 11:04 AM
Arsenic
Arsenic - avatar
+ 1
Arsenic can you explain me more clearly? Cause what I've been thinking is that we return a pointer and so only the "address" of the variable is returned. So when we dereference the address, aren't we trying to access a value which is destroyed already?
31st May 2021, 3:59 PM
Rishi
Rishi - avatar
+ 1
Arsenic if my understanding is correct, then the value (for example 40) will be considered like an address. So we'll be able to dereference it(40)(though it might give garbage values or segmentation fault). I mean, we can dereference it without getting a compiler error. Am I right?
1st Jun 2021, 2:17 AM
Rishi
Rishi - avatar
+ 1
Rishi yes, you can hardcode pointer by doing something like this int *ptr = (int *) 40; Although it would be undefined behaviour if you try to deterrence it.
1st Jun 2021, 2:59 AM
Arsenic
Arsenic - avatar
+ 1
Arsenic thank you man! You just solved a big doubt for me! 🥺🥺🥺
1st Jun 2021, 4:00 AM
Rishi
Rishi - avatar
+ 1
Here is a tutorial on Variables Scope Rules in C Programming https://www.techcrashcourse.com/2015/05/c-programming-variables-scope-rules.html The scope of a variable defined the sections of a program from where a variable can be accessed. The scope of variable depends upon it's place of declaration.
26th Jun 2022, 8:30 AM
Arun Kumar
Arun Kumar - avatar
0
Unknown I don't understand, what are you trying to say?
26th Jul 2021, 1:29 AM
Rishi
Rishi - avatar
- 1
If you have a problem in the first question in C => Functions => Pointers, I have something that might help. The letter after percentage mark is letter x
25th Jul 2021, 10:49 PM
Unknown