1

I am currently working on a C program where I am trying to pass an pointer as an argument into a scanf.

Example:

int main() { 
    int *num;
    scanf("%d", num);
    printf("%d", *num);
}

Considering that the typical scanf passes in an address to store a value I don't see why it cannot store the address a pointer holds. I receive a memory error when I try to run this block of code. What could possibly be the issue?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Bwoods
  • 185
  • 1
  • 3
  • 13
  • 2
    I'd recommend "man scanf". You should find the answer quite plain. – Bruce K Mar 18 '16 at 21:31
  • 1
    Please clarify **which** address `num` holds! – too honest for this site Mar 18 '16 at 21:42
  • 2
    Passing a pointer is fine, except you forgot to initialize it. `num` is undefined, so you're passing a random, unknown pointer value to `scanf`. Just initialize `num` to the address of the `int` variable that you want to use for the value, and then it will work. Just declare something like `int val;` and then assign `num = &val;` to initialize `num`. – Tom Karzes Mar 18 '16 at 21:49
  • You've just discovered why pointers are hard: it's not so much that pointers are hard, as that memory allocation can be tricky to keep track of. Whenever you use pointers, you have to think about *where does the pointer point* and *is there enough valid memory allocated there for what I'm doing*? There's no mechanism in C to magically allocate memory for pointers to point to; you almost always have to take care of it yourself. – Steve Summit Mar 18 '16 at 22:01

1 Answers1

11

You can do that only if num points to valid memory.

// Not OK
// Leads to undefined behavior since num does not point
// to anything valid.
int* num;
scanf("%d",num);

// OK
int* num = malloc(sizeof(*num));
scanf("%d",num);

// Also OK
int i;
int* num = &i;
scanf("%d",num);
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • 1
    I consider it better style to group the declarator the way the language intends, i.e. `int *i` rather than the misleading `int* i`. `int*` is not syntactically a type. Rather, it is a type and part of a declarator, the latter of which will bind more tightly to whatever follows, and not apply to any additional variables that may follow. – Tom Karzes Mar 18 '16 at 21:54
  • 1
    Why can't num point to garbage memory? After all it is still an address. – Bwoods Mar 18 '16 at 22:29
  • 2
    It can point to garbage memory. But it will *usually* point to memory that hasn't even been allocated to your process, and that's why you're getting your error when you run it. – Crowman Mar 18 '16 at 22:45
  • 1
    @Bwoods, if `num` has not been assigned the address of an object, then evaluating the expression `*num` produces undefined behavior. You can luck out, and have the UB manifest as if `num` did point to an object, but it might do all manner of other things. Among the more likely are crashing the program and, worse, silently corrupting your program's data. And which manifests is not necessarily consistent. – John Bollinger Sep 14 '18 at 19:01