Linux Newbie help
 help / color / mirror / Atom feed
From: sandeep lahane <sandeep.lahane@gmail.com>
To: mayur nande <mayur.nan@gmail.com>
Cc: Rick Brown <rick.brown.3@gmail.com>,
	kernelnewbies <kernelnewbies@nl.linux.org>,
	linux-newbie@vger.kernel.org
Subject: Re: C question
Date: Thu, 8 Oct 2009 11:13:32 +0530	[thread overview]
Message-ID: <2d51cbf80910072243u3778e24dj89c7b7f29903d67b@mail.gmail.com> (raw)
In-Reply-To: <f86e1f200910072002o3a08ca64h4a9bc70dd33d0e7c@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1695 bytes --]

On Thu, Oct 8, 2009 at 8:32 AM, mayur nande <mayur.nan@gmail.com> 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@gmail.com> 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@nl.linux.org
>> 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.

[-- Attachment #2: Type: text/html, Size: 2678 bytes --]

      reply	other threads:[~2009-10-08  5:43 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-10-08  2:12 C question Rick Brown
2009-10-08  2:52 ` Manish Katiyar
2009-10-08  5:37   ` Kalpesh Rathod
2009-10-08  8:48   ` Michał Nazarewicz
2009-10-08  3:02 ` mayur nande
2009-10-08  5:43   ` sandeep lahane [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2d51cbf80910072243u3778e24dj89c7b7f29903d67b@mail.gmail.com \
    --to=sandeep.lahane@gmail.com \
    --cc=kernelnewbies@nl.linux.org \
    --cc=linux-newbie@vger.kernel.org \
    --cc=mayur.nan@gmail.com \
    --cc=rick.brown.3@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox