jQuery in Action, 3rd edition
The moose likes C / C++ and the fly likes pointers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Languages » C / C++
Bookmark "pointers" Watch "pointers" New topic


aaka jain

Joined: May 16, 2013
Posts: 12
void *vpointer;
char *charpointer="google";
printf("%s",(char*)vpointer +3);

i was having difficulty understanding these lines of code,these are from a c program,could somebody please help me to understand these by giving a detail explanation (especially the printf statement )
thank you
Ramesh Pramuditha Rathnayake
Ranch Hand

Joined: Oct 31, 2012
Posts: 178

vpointer can hold any type of pointer value as it is void...
charpointer stores the memory location of g character. (the first letter).

The size of a char is 1 byte. Therefore when we increase the value of char pointer by one, it will change the address location by one byte.

(char*)vpointer --> This is now a char pointer.
(char*)vpointer +3 --> Now this is the address is increased by 3. Therefore this represent the address of g (The fourth letter)

When we store a string in a char pointer, it will add value zero char at the end.(To recognize the end of the string). Therefore actually google is stored in memory as 0,101,108,103,111,111,103
When we tell printf to print a string (Using %s), this will determine the end using zero.
Only 0,101,108,103 are in the new string. Therefore this prints 'gle' only..

I agree. Here's the link: http://aspose.com/file-tools
subject: pointers
It's not a secret anymore!