wood burning stoves 2.0*
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Languages » C / C++
Bookmark "pointers" Watch "pointers" New topic
Author

pointers

aaka jain
Greenhorn

Joined: May 16, 2013
Posts: 12
void *vpointer;
char *charpointer="google";
vpointer=charpointer;
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: 169
    
    1

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


Ramesh-X
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: pointers
 
Similar Threads
Hi, I need Help to convert String to Binary
printf in Java ?
System.out.printf("%1$c",65536);
Problem in C
How the pointer represent in the case of String and Char