Quantcast
Channel: Segmentation Fault, pointer issue? - Stack Overflow
Viewing all articles
Browse latest Browse all 3

Answer by HelloWorld123456789 for Segmentation Fault, pointer issue?

$
0
0

In main you have to allocate spaces for x and y before you can use it. If x and y are not allocated spaces, they point to arbitrary memory locations. If they are outside of your program segment, you will get a segmentation fault.

main(){   int* x=malloc(sizeof(int));   *x = 3;   int* y=malloc(sizeof(int));   *y = 4;   int z = fun(x, y);   printf("%d", z);}

Or like this:

main(){   int x=3,y=4;   int z = fun(&x, &y);   printf("%d", z);}

Viewing all articles
Browse latest Browse all 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>