[Solved] Unable to produce wanted output, loops and arrays in C [closed]


I am sorry but scanf does not work that way. To write the value the user types in at the prompt, as n[i], scanf needs an address (in memory).

C is in this regard intentionally rudimentary, more so than a language like Python or JavaScript.

The way you wrote your code, you end up passing garbage (whatever integer value is contained at the address n + i) as an argument to scanf and expect the machine to replace that garbage with the value. And it cannot do that.

An apt analogy would be if I asked you to take whatever you find by the door and put it on a shelf, but instead of telling you which shelf to put it on, I’d give you the object that is currently on the shelf. You’d be right to ask me “why did you give me this, and which shelf do I put that what I find by the door on?”.

When you specify a variable as an argument to a procedure, what is passed is the value contained in the space allocated for the variable, commonly called the “variable value”, of course.

You need to give scanf the address of the space to write to. The address of n[i] is obtained using the unary operator &, e.g. &n[i] or n + i.

2

solved Unable to produce wanted output, loops and arrays in C [closed]