I am confused with the lines 19 20. Since "pointsArray" is a pointer, and it stores address, why in "scanf(" %d", &pointsArray);" , why not using “scanf("%d",pointsarray)” From line 20, it indicates pointsArray= an integer instead of a address ?
Thanks a lot for the help !
Anyone help with the code in tutorial 48 please !! Pointers confusion
Post a Reply
|Oldest Newest Rating|
· January 22, 2016
Thanks so much for your reply, it clears my question now !
· January 23, 2016
Your confusions are numerous. The one you're asking about here is precedence. Think about the precedence of operators like multiplication, addition, division and subtraction... Now think about what the precedence of pointer[subscript] and &address-of might be; they might be the reverse of what you're thinking.
The pointer[subscript] operation follows the pointer link so the expression pointsArray[i]. pointsArray[i] is an int. Hence the ampersand to form a pointer to that int object. &pointsArray[i] can also be written pointsArray + i; this way there are no pointer[subscript] or &address-of operators to confuse you.
scanf needs a pointer to an int to be able to modify the int that is pointed to. I have an exercise for you:
Your first task is to explain how it could be that the first call to change() would modify the integer constant 0...
Once you've done that, note that the first call to change() can't possibly change the constant 0. Run the code and see for yourself...
Next, work out how to make the change function modify x in a way that main can see. You'll know you've succeeded when the value of x after the call prints 42 instead of 0.
If you need a hint, start by looking up pass-by-value in Google (THIS IS IMPORTANT!)... then try using pointers.
One of the most popular languages of all time.
|Bucky Roberts Administrator|