From: Manish Katiyar <mkatiyar@gmail.com>
To: Rick Brown <rick.brown.3@gmail.com>
Cc: kernelnewbies <kernelnewbies@nl.linux.org>, linux-newbie@vger.kernel.org
Subject: Re: C question
Date: Thu, 8 Oct 2009 08:22:31 +0530 [thread overview]
Message-ID: <ea11fea30910071952x58720190r9fef930ff524c849@mail.gmail.com> (raw)
In-Reply-To: <7783925d0910071912u5e4f4a2au8442cb6537cd35ed@mail.gmail.com>
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:
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.
/tmp> gcc a.c
a.c: In function ‘main’:
a.c:5: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘void *’
a.c:6: warning: dereferencing ‘void *’ pointer
a.c:6: error: invalid use of void expression
/tmp> cat a.c
#include <stdio.h>
int main()
{
void *ptr = 0;
printf("%d \n", ptr+1);
printf("%d \n", *(ptr+1));
}
>
> [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
>
>
--
Thanks -
Manish
==================================
[$\*.^ -- I miss being one of them
==================================
--
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
next prev parent reply other threads:[~2009-10-08 2:52 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 [this message]
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
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=ea11fea30910071952x58720190r9fef930ff524c849@mail.gmail.com \
--to=mkatiyar@gmail.com \
--cc=kernelnewbies@nl.linux.org \
--cc=linux-newbie@vger.kernel.org \
--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