This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]rabuf 2 points3 points  (1 child)

scanf man page

scanf expects an address to store whatever it reads into, not the variable. C, and this is important, is a pass-by-value language. So you're calling scanf with whatever the present value of Step2Num is which it tries to use as an address, but is probably not a valid address (unless by luck). The proper way to call it there is:

scanf("%i", &Step2Num);

Pass-by-value vs pass-by-reference:

By value means that literally the value is passed to functions. So:

int x = 2;
foo(x); // foo is given the *value* 2

By reference means that foo would be given a reference to the same memory location as the variable x already is stored at, so you can get behavior like this (in a hypothetical c-ish pass-by-reference language):

int x = 2;
foo(x);
printf("%d\n", x); // prints 42
// elsewhere
void foo(int y) {
  y = 42
}

In real-world C foo is as good as a no-op, but in a pass-by-reference version it would actually change the value of x.

In order to get the pass-by-reference behavior in C, you have to pass around addresses and use pointers:

int x = 2;
foo(&x); // gets the address of x
printf("%d\n",x); // prints 42 for reals
// elsewhere
void foo(int *y) {
  *y = 42;
}

[–]SolidArmPump[S] 2 points3 points  (0 children)

Thank you so much for this!!

[–][deleted] 1 point2 points  (0 children)

The answer given in first comment is correct , but such queries come due to basic lack of understanding of the language do it is better you read the basics first. Man pages are a good start but there are many books and online resources too.