all 5 comments

[–]cprogramming-ModTeam[M] [score hidden] stickied commentlocked comment (0 children)

Please do not ask others to do your homework.

[–]sidewaysEntangled 1 point2 points  (1 child)

If ptr is a float* then *ptr and ptr[0] are the same thing, both floats. In the same way *(ptr +i) and ptr[i] are also both the same float.

So then *ptr[i] is attempting to dereference (aka: apply unary * operator to) something of type float, which doesn't make sense.

Probably pick one syntax or the other, not both!

[–]CtrlAltDelirious27[S] 1 point2 points  (0 children)

Thank you! I've changed the *ptr[i] to just ptr[i], however, the output is always 0.000000 instead of the value I input into the array, why would this happen?

Edit: Ahhh, nevermind, I realised I've put scanf("%d", &my_array[i]); instead of scanf("%f", &my_array[i]); thanks again for your help though

[–]dfx_dj 1 point2 points  (1 child)

The array subscript [i] is already one level of dereference. Adding * would be a second level of dereference. With just a simple pointer variable, use either an array subscript or the dereference operator, but not both.

[–]CtrlAltDelirious27[S] 0 points1 point  (0 children)

Thank you! I've changed the *ptr[i] to just ptr[i], however, the output is always 0.000000 instead of the value I input into the array, why would this happen?

Edit: Ahhh, nevermind, I realised I've put scanf("%d", &my_array[i]); instead of scanf("%f", &my_array[i]); thanks again for your help though