1

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.

4
  • 1
    b is a uninitialized pointer. Derefencing it will cause undefined behavior Commented Oct 17, 2019 at 11:37
  • So, what would be the correct syntax for this case? Commented Oct 17, 2019 at 11:39
  • One thing you must always know about your pointers is where they point. In your program, the pointer b is 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 place b points to (or whether it's a valid value), but b itself is valid and points to a definite and known place. Commented Oct 17, 2019 at 11:43
  • @ThomasSablik - it's not necessary to dereference it to get undefined behaviour. Accessing its value gives undefined behaviour. The simple act of passing it by value to a function (by definition) accesses it value. (A precursor to dereferencing a pointer is also to access its value). Commented Oct 17, 2019 at 12:36

1 Answer 1

4

*b is not pointing anywhere. Try this:

#include <stdio.h>
#include <stdlib.h>
int main ()
{
  int *b = malloc(sizeof(int)); // dynamically allocate memory
  printf ("Enter some int value -> ");
  scanf ("%d", b);

  printf ("Entered value is: %d", *b);
  free(b); // free the dynamically allocated memory
  return 0;
}

Or without dynamic memory allocation by pointing to a variable on the stack instead:

#include <stdio.h>
int main ()
{
  int a;
  int *b = &a;
  printf ("Enter some int value -> ");
  scanf ("%d", b);

  printf ("Entered value is: %d", *b); // or have a instead of *b
  return 0;
}

Make sure to consider the warnings that your compiler gives you. Your code will probably trigger a warning about using the uninitialized local variable 'b'. If you didn't get a warning, see if you can configure your compiler to be more strict.

Sign up to request clarification or add additional context in comments.

1 Comment

... by adding -Wall -Wextra -pedantic to your compiler flags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.