Re: pointer addition and structs
From: Mark Randall (strike_at_rapiercom.freeserve.co.uk)
Date: 12/16/04
- Next message: Raheem: "Re: LDAP Connection"
- Previous message: Steve Friedl [MVP/Security]: "Re: pointer addition and structs"
- In reply to: Steve Friedl [MVP/Security]: "Re: pointer addition and structs"
- Next in thread: Severian: "Re: pointer addition and structs"
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 16 Dec 2004 02:30:38 -0000
My bad, shouldnt have changed it to long (which I did only at the end, was
char* before) to start with.
Doh, should have known that from using pointers for strings.
- Mark R
"Steve Friedl [MVP/Security]" <steve@unixwiz.net> wrote in message
news:eTqF%23ax4EHA.524@TK2MSFTNGP09.phx.gbl...
> "Mark Randall" <strike@rapiercom.freeserve.co.uk> wrote in message
> news:u4GjN2w4EHA.1296@TK2MSFTNGP10.phx.gbl...
>> foo f;
>> long* p = &f + 16;
>
> No, not at all, because pointer math is scaled by the size of the
> pointed-to object: you're calculating an address that is far beyond the
> bounds of the base structure "f". In raw terms, you are calculating:
>
> long *p = &f + (16 * sizeof f);
>
> which is required to support array notation. In C, saying
>
> ary[5]
>
> is precisely the same as
>
> *(ary + 5)
>
> Really: it's positively identical, so this scale-by-size has to be there
> or this property would not work.
>
> Curiously, this can be extrapolated such that these are all identical:
>
> ary[5]
> *(ary + 5)
> *(5 + ary)
> 5[ary]
>
> The latter is perfectly valid and well-formed C, though it's probably not
> recommended.
>
> Steve
> ---
> Steve Friedl -- Tustin, California USA -- www.unixwiz.net
> Unix Wizard -- Microsoft MVP/Security -- I speak for me only
>
- Next message: Raheem: "Re: LDAP Connection"
- Previous message: Steve Friedl [MVP/Security]: "Re: pointer addition and structs"
- In reply to: Steve Friedl [MVP/Security]: "Re: pointer addition and structs"
- Next in thread: Severian: "Re: pointer addition and structs"
- Messages sorted by: [ date ] [ thread ]