void pointer arithmatic

N

nileshsimaria

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
 
J

Jens Thoms Toerring

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 said:
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
 
C

Chris Dollin

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.
 
A

Andrey Tarasevich

Jens said:
...

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.
 
J

Jack Klein

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top