+ 8

So, in this code did the address of pointer k become -5?

#include <stdio.h> void func(int* x){ x-=5; } int main() { int k=7; func(&k); printf("%d",k); return 0; }

28th Jan 2019, 6:07 PM
Zhenis Otarbay
Zhenis Otarbay - avatar
4 Respostas
+ 3
Nothing really happens. x in func had the address of k and then contains the address of 5 bytes before that. However, you never use it so it doesn't have any lasting effect as x's value is a local and not returned.
28th Jan 2019, 11:10 PM
John Wells
John Wells - avatar
+ 4
John Wells doesn't x have 5 bytes * sizeof(int) value in func ?
30th Jan 2019, 1:19 PM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
func() was not return int or pointer. first:void must be -> int or returnable value. second: func(){... return x;} third: k=func(&k) and then you take response from program.
28th Jan 2019, 11:16 PM
Ümit YAVUZ
Ümit YAVUZ - avatar
+ 2
Baptiste E. Prunier yes, I forgot to look at the type of x so likely 20 bytes before k.
30th Jan 2019, 5:24 PM
John Wells
John Wells - avatar