Re: C question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




On Thu, Oct 8, 2009 at 11:13 AM, sandeep lahane <sandeep.lahane@xxxxxxxxx> wrote:




On Thu, Oct 8, 2009 at 8:32 AM, mayur nande <mayur.nan@xxxxxxxxx> wrote:
Hi Rick,

Some days ago i had the same question in my mind. While going through "The Linux Kernel Architecture" book (by Wolfgang Mauerer), i got the answer:

The GNU compiler supports arithmetic with void pointers as well as function pointers. The increment step is 1 byte. These are used by the kernel at various points.

Have fun.

Regards
Mayur

On Thu, Oct 8, 2009 at 7:42 AM, Rick Brown <rick.brown.3@xxxxxxxxx> wrote:
Hello list,

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++"? But then how about this:

[rick@linux rick]$ cat t.c
#include <stdio.h>
int main()
{
   void *ptr = 0;
   printf("%d \n", ptr+1);
}
[rick@linux rick]$ gcc t.c
[rick@linux rick]$ ./a.out
1
[rick@linux rick]$

It compiles and runs fine ... !

TIA,

Rick

--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ




Arithmetic on void and function pointers is part of GNU C extensions, C standard
does not support it (size of void and functions is taken as 1). There are many such
GNU extensions which are used in kernel. One way to figure out which all extensions
are used is by providing -pedantic flag, this will emit warning for such usage.

I think many of these extensions have became part of C99 standard already.


Regards,
Sandeep.
 


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux