By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,335 Members | 1,814 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,335 IT Pros & Developers. It's quick & easy.

void pointer arithmatic

P: n/a
Hi,

A void pointer can point to any object. Compiler doesnt know size of
the object being pointed by void pointer thats why ++ is not allowed in
void pointer.

I have small C programme compiled with gcc.

int main()
{
int i;
void *vptr = &i;

printf("%x", vptr);
vptr++'
printf("%x", vptr);
return 0;
}

Above code is printing : ffbef8dc and ffbef8dd
vptr++ is pointing to next byte.

Not sure why gcc is not giving any error or warning. Can someone throw
internal details.

Thanks,
Nilesh

Nov 13 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ni***********@gmail.com wrote:
A void pointer can point to any object. Compiler doesnt know size of
the object being pointed by void pointer thats why ++ is not allowed in
void pointer.
I have small C programme compiled with gcc.
#include <stdio.h>
int main()
int main( void )

since your main() doesn't take arguments.
{
int i;
void *vptr = &i;
printf("%x", vptr);
printf( "%p", vptr );

"%p" is the correct format specifier for (void) pointers.
vptr++'
vptr++;
printf("%x", vptr);
return 0;
}
Above code is printing : ffbef8dc and ffbef8dd
vptr++ is pointing to next byte.
Not sure why gcc is not giving any error or warning. Can someone throw
internal details.
This is an extension gcc makes to the C language - it treats void
pointers like char pointers when adding to or subtracting from such
pointers, i.e. it assumes that what they point to has size 1. If you
invoke it with '-ansi' and '-pedantic' or just '-Wpointer-arith' it
at least warns you about "wrong type argument to increment" for the
line where vptr gets incremented.

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://toerring.de
Nov 13 '06 #2

P: n/a
ni***********@gmail.com wrote:

A void pointer can point to any object. Compiler doesnt know size of
the object being pointed by void pointer thats why ++ is not allowed in
void pointer.

I have small C programme compiled with gcc.

int main()
{
int i;
void *vptr = &i;

printf("%x", vptr);
vptr++'
printf("%x", vptr);
return 0;
}

Above code is printing : ffbef8dc and ffbef8dd
vptr++ is pointing to next byte.

Not sure why gcc is not giving any error or warning.
It's a gcc extension. I expect you forgot the -ansi
-pedantic options on the command line.

--
Chris "hantwig efferko VOOM!" Dollin
A rock is not a fact. A rock is a rock.

Nov 13 '06 #3

P: n/a
:
vptr++;

You're using a K compiler, not a C compiler.

You can't perform arithmetic on a void*.

--

Frederick Gotham
Nov 13 '06 #4

P: n/a
Jens Thoms Toerring wrote:
...
>int main()

int main( void )

since your main() doesn't take arguments.
>{
A matter of personal style, when used in function definition context. There's no
difference between 'int main()' and 'int main(void)' is such a context.

--
Best regards,
Andrey Tarasevich
Nov 13 '06 #5

P: n/a
On Mon, 13 Nov 2006 16:11:45 -0800, Andrey Tarasevich
<an**************@hotmail.comwrote in comp.lang.c:
Jens Thoms Toerring wrote:
...
int main()
int main( void )

since your main() doesn't take arguments.
{

A matter of personal style, when used in function definition context. There's no
difference between 'int main()' and 'int main(void)' is such a context.
....unless you call it recursively, in which case the former does not
provide a prototype for error checking, whereas the latter does.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.