#pointer and array execution

35 messages · Page 1 of 1 (latest)

sweet parrot
#
#include<stdio.h>
int main() { //*ptr = &a


float values[6] = {10.5, 100.9, 200, 4.1, 989.38, 2.3};
float *ptr;
ptr = values;    // OR ptr = &values[0]

printf("%f" , *(ptr+4)) ;

return 0;
}```
pulsar charmBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

sweet parrot
#

is there any other way to print values[4]
instead of *(ptr + 4)

supple vector
#

that's what [] does

#

it's an offset from the first element

sweet parrot
#

can't get what you want to say

supple vector
#

values[4] = &values[0] + 4

#

I mean if you want

#

you could make the pointer point to the end of the array

#

and offset backwards

sweet parrot
#

am i right ?

supple vector
#

general gist yeah

#

it's 4 elements ahead

sweet parrot
#

using ptr varliable

#

any other way

#

like

#

ptr[4] or *ptr[4]

supple vector
#

yeah

#

the pointer points to the beginning of the array

sweet parrot
#

or there is only one way
*(ptr +2)

supple vector
#

so you can use the [] operator the same way

sweet parrot
supple vector
#

because ptr points to the beginning of values

values[4] = ptr[4]

sweet parrot
#

did i got right ?

supple vector
#

yeah

#

they're the same thing

#

[] is for convenience and looks nicer

sweet parrot
#

f my vs code is shit
few minutes ago it was showing wrong output on using ptr[4]

sweet parrot
supple vector
pulsar charmBOT
#

@sweet parrot Has your question been resolved? If so, run !solved :)

sweet parrot
#

!solved