this one is from a job interview.
#include <stdio.h>
#include <stdlib.h>
void func(int* x)
{
x =(int*)malloc(sizeof(int));
*x = 17;
}
void main() {
int y = 42;
func(&y);
printf("%d", y);
}
The question is what will be printed and what's wrong here.
I saw one solution that claimed that x is a local variable so the 42 will be printed and the problem is the memory leak. But I don't think so, since I pass the address of y. It seems to me that the use of int as a pointer (where one is 4 bytes and the other is 8 bytes) is actually the problem. Can you give your opinion about it?
Thanks