Step 1: const char *s = ""; The constant variable s is declared as an pointer to an array of characters type and initialized with an empty string.
Step 2: char str[] = "Hello"; The variable str is declared as an array of charactrers type and initialized with a string "Hello".
Step 3: s = str; The value of the variable str is assigned to the variable s. Therefore str contains the text "Hello".
Step 4: while(*s){ printf("%c", *s++); } Here the while loop got executed untill the value of the variable s is available and it prints the each character of the variable s.
Here 'str' points to location of 'H'.When s = str ,'s' contains location of 'H'.'*s' points to 'H' and each time it increments it prints "HELLO".
Mohini said:
(Tue, Jan 18, 2011 10:20:37 AM)
while(*s)
this condition says that while loop will get executed till there is no NULL,i.e end of string.
so it will go on printing each character till the end of string.
thus whole string gets printed
Madhu said:
(Thu, Jul 21, 2011 02:55:22 PM)
Here *s is a constant pointer how can it's value be changed from 'NULL' to 'str' .
Am12Cs said:
(Wed, Aug 31, 2011 10:46:20 PM)
Mohini concept is correct, while loop be excuted untill its value comes to null.
Pallavi said:
(Fri, Sep 23, 2011 07:25:30 PM)
Here s points to the empty string and it is a constant pointer, then how can we assign string 'str' to 's'?
Chandu said:
(Wed, Oct 5, 2011 08:49:35 PM)
Here they are allocating location address of characters but till it is constant pointer we can't change its values.
Khushi said:
(Fri, Dec 2, 2011 02:54:19 PM)
Can anyone explain this in detail? how can we assign a const pointer different values each time?
Nuzhat Memon said:
(Tue, Dec 6, 2011 01:03:12 PM)
Const pointer can change its value not an address.