#1  
Old 3rd May 2007, 16:45
neothelord neothelord is offline
Junior Member
 
Join Date: Apr 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default C Programming Quiz #2

Guys,

The second quiz in C programming (10 multiple choice questions with feed back) is ready.

http://mulogic.phpnet.us/drupal/?q=C+programming-Quiz+2

Rate your C skills.
Reply With Quote
Sponsored Links
  #2  
Old 7th May 2007, 01:29
nixen nixen is offline
Junior Member
 
Join Date: May 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sorry my bad english but i can't belive that i'm studying c++ language for over 2 year at school and i nerver see code like this :
void main()
{
char *s[]={ "miller","miller","filler","filler"};
char **p;
p=s;
printf("%s ",++*p);
printf("%s ",*p++);
printf("%s ",++*p);
}

can you explain me how it works? thanks
Reply With Quote
  #3  
Old 7th May 2007, 07:55
neothelord neothelord is offline
Junior Member
 
Join Date: Apr 2007
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

here s is an array of 4 character pointers. so when you refer an array by base name, it decays to a pointer to the first element . hence basename s is char **.

so ptr is a pointer to the first element of the array. when you dereference it with a *, you get the value stored in the first element of the array, which is the pointer to the first string "miller". The prefix ++, increments this pointer by 1. since the data type is char *, the pointer is incremented to point the next byte(second char of the string "miller"). and the first printf prints "iller"

In the second printf postfix ++ has more precedence than *. so it increments ptr to point to second element of s. but remember, this side effect is applicable only after a sequence point. so the *ptr, still yields the value at the first element of s.

when control comes to the third ptr, ptr is pointing to the second elemtn of s. here we do a normal dereference to get the address of the second string "miller", and increment it by a char. so this also prints "iller".

The third and fourth string "filler" are really fillers as the name implies.
Reply With Quote
  #4  
Old 8th May 2007, 23:38
nixen nixen is offline
Junior Member
 
Join Date: May 2007
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
 
Default

Thanks The pointer are the base of c++ language but in my life i don't use ** pointer, now i understand
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How much you know in C - (C Quiz) neothelord Programming/Scripts 0 29th April 2007 16:21


All times are GMT +2. The time now is 05:03.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2013, vBulletin Solutions, Inc.