Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

What will those strings print out?

Status
Not open for further replies.

mickey2005

Banned
Joined
Sep 25, 2005
Messages
0
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,280
Activity points
0
1. What will print out?
2. main()

3. {

4. char *p1="name";

5. char *p2;

6. p2=(char*)malloc(20);

7. memset (p2, 0, 20);

8. while(*p2++ = *p1++);

9. printf("%s\n",p2);

10. }

Answer:empty string.
 

Re: what will print out?

but it should print the "name"
 

Re: what will print out?

At the end of the code, p2 points to NULL character (as while loop increments p2 and runs till *p2 == 0). As soon as p2 points to NULL , while loop exits.
So while printing it prints NULL string.
 

Re: what will print out?

Hai,

The s2 pointer should reach its original address.
You should substract the number of locations incremented.
After substracting you get what you desired.

Regards
Gopi
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top