Re: C question
Michał Nazarewicz <[email protected]>
| Newsgroups | gmane.linux.newbie,gmane.linux.kernel.kernelnewbies |
|---|---|
| Organization | Samsung Electronics |
| Message-ID | <op.u1g32byw7p4s8u@localhost> |
> On Thu, Oct 8, 2009 at 7:42 AM, Rick Brown <[email protected]> wrote: >> As far as I recall from K&R, isn't pointer arithmetic on a void >> pointer banned? And any effort to do that results in an error - >> because the compiler won't know by how much size to increment the >> pointer for a statement like "ptr++"? On Thu, 08 Oct 2009 04:52:31 +0200, Manish Katiyar <[email protected]> wrote: > But in the program, you aren't actually trying to dereference the > value. Just adding means it becomes normal arithmetic and that is why > you get result as 1. You will see the error if you try to dereference > it. This comment is a bit misleading. The standard does not define behaviour of pointer arithmetic on pointer to void. What one need to realise is that undefined behaviour means compiler's documentation may well define how such a construct is evaluated and gcc (with proper options) decides to treat pointer to void as if sizeof(void) == 1. So the thing it's true pointer arithmetic on a pointer to void is undefined behaviour as far as C standard is concerned however because Linux is compiled with gcc kernel's developers tend to make use of gcc's extensions and one of it is arithmetic on a pointer to void. -- Best regards, _ _ .o. | Liege of Serenely Enlightened Majesty of o' \,=./ `o ..o | Computer Science, Michał "mina86" Nazarewicz (o o) ooo +---<[email protected]>---<[email protected]>---ooO--(_)--Ooo-- -- To unsubscribe from this list: send the line "unsubscribe linux-newbie" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.linux-learn.org/faqs