Categories:
.NET (961)
C (387)
C++ (185)
CSS (84)
DBA (8)
General (31)
HTML (48)
Java (641)
JavaScript (220)
JSP (109)
JUnit (31)
MySQL (297)
Networking (10)
Oracle (562)
Perl (48)
Perl (9)
PHP (259)
PL/SQL (140)
RSS (51)
Software QA (28)
SQL Server (5)
Struts (20)
Unix (2)
Windows (3)
XHTML (199)
XML (59)
Other Resources:
Using Pointers
What print out will the folloging code produce?
main() { char *p1=“name”; char *p2; p2=(char*)malloc(20); memset (p2, 0, 20); while(*p2++ = *p1++); printf(“%s\n”,p2);> }
✍: Guest
The pointer p2 value is also increasing with p1
.
*p2++ = *p1++ means copy value of *p1 to *p2 , then
increment both addresses (p1,p2) by one , so that they
can point to next address . So when the loop exits (ie
when address p1 reaches next character to “name” ie
null) p2 address also points to next location to
“name” . When we try to print string with p2 as
starting address , it will try to print string from
location after “name” … hense it is null string ….
eg :
initially p1 = 2000 (address) , p2 = 3000
*p1 has value "n" ..after 4 increments , loop exits …
at that time p1 value will be 2004 , p2 =3004 … the
actual result is stored in
3000 - n , 3001 - a , 3002 - m , 3003 -e … we r trying
to print from 3004 …. where no data is present … thats
why its printing null .
Answer:empty string.
2007-02-26, 5896👍, 0💬
Popular Posts:
.NET INTERVIEW QUESTIONS - Is versioning applicable to private assemblies? Versioning concept is onl...
What is Native Image Generator (Ngen.exe)? The Native Image Generator utility (Ngen.exe) allows you ...
Can you explain different software development life cycles -part II? Water Fall Model This is the ol...
What is XSLT? XSLT is a rule based language used to transform XML documents in to other file formats...
Why is there extra white space before or after tables? This is often caused by invalid HTML syntax. ...