• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

pointers

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 178
2
Netbeans IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic