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

Segmentation Fault, pointer issue?

$
0
0

I'm pretty new to C coding, and pointers are something I'm having a lot of trouble with. I'm trying to write a program that takes in 2 pointers as parameters, adds the value of the first to the second, then returns the value of the first. What I've written is this:

int foo(int *g, int *h){     int a;    a = *g;     *h += a;    return a; }

However, I am getting a segmentation fault error using an online compiler. I read that these are caused by rogue pointers, but I'm not sure where the error is. Can someone help?

EDIT:I'm calling this function this way:

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

I thought that was the way to declare a pointer, and that using (*) to dereference it was how to assign it a value. Am I incorrect?


Viewing all articles
Browse latest Browse all 3

Trending Articles



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