I'm new to C, and for a few months I've used pointers and tried to figure out how they work and learn syntax for using them, but i ran into a confusing part when compiler throws an error
[1] 1473 segmentation fault (core dumped) ./a.out
but i think code syntax is correct.
Since I am working with and learning about dynamic data structures, a lot of times everything seems fine, but occasionally i end up into some weird errors, so i provided this simple example of using pointers which confuses me.
#include <stdio.h>
int main ()
{
int *b;
printf ("Enter some int value -> ");
scanf ("%d", b);
printf ("Entered value is: %d", *b);
return 0;
}
I am declaring a variable which is pointer to a integer in memory. Since its pointer in scanf() argument is just name of that variable, and when i print it to stdout(printf()) i use *b since pointer to pointer is actual value of that variable.
bis a uninitialized pointer. Derefencing it will cause undefined behaviorbis uninitialized, so you have no idea where it points to (or whether it's even valid). Try making it point to the middle of an array:int arr[3]; int *b = &a[1];. Now you know where it points! You don't know what is in the placebpoints to (or whether it's a valid value), butbitself is valid and points to a definite and known place.