Exforsys

Online Training

Puzzled on Output

This is a discussion on Puzzled on Output within the C and C++ forums, part of the Programming Talk category; My C code is as below: #include <stdio.h> int main() { char *a, *b; x = "WELCOME"; ...


Go Back   Exforsys > Programming Talk > C and C++

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-23-2007, 12:11 AM
Senior Member
 
Join Date: Apr 2006
Posts: 141
Adrian is on a distinguished road
Exclamation Puzzled on Output

My C code is as below:

#include <stdio.h>
int main()
{
char *a, *b;
x = "WELCOME";
b = test(a);
printf ("b = %s \n", b);
return 0;
}

char* test(char *p1)
{
p1 += 3;
return (p1);
}


The output I got was COME .I could not understand how COME was printed as output. Can someone explain me on the output evaluation?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 05-23-2007, 01:47 AM
Junior Member
 
Join Date: May 2007
Posts: 9
hvg_99 is on a distinguished road
Puzzled on output: it is not true

#include <stdio.h>
int main()
{
char *a, *b;
a = "WELCOME";
b = test(a);
printf ("b = %s \n", b);
return 0;
}

char* test(char *p1)
{
p1 += 3;
return (p1);
}

First of all you have used 'x' as a variable...which is wrong instead it must be a="WELCOME".

you are calling a function called test where you are sending the pointer which is pointing to "WELCOME". *p1 is currently pointing to 'W' of "WELCOME" which is '0' since arrays in 'C' starts from 0; the body says

p1 +=3; meaning you are incrementing p1 4 times i.e. p1=0, p1=1,p1=2 then 3 which means it currently points to 'C' of 'WELCOME". and now u are returning p1.

and since the output is b=COME which is not a puzzle..it is so simple .

I hope i have cleared your doubt.

Regards,
Gayatri
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-24-2007, 04:30 PM
Senior Member
 
Join Date: Apr 2006
Posts: 145
Rahulbatra is on a distinguished road
This is a nice question and program reflecting the basic concepts of pointers and strings. The base or the starting address of string is incremented by 3. Say for instance if the base address is 1000 then 1000+3=1003.The storage inside the memory of the string would be as below:
1000 - W
1001 - E
1002- L
1003- C
1004- O
1005- M
1006- E

Since the current pointer now is placed in 1003 all characters from 1003 of the string gets printed and so you got output as COME
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads

Thread Thread Starter Forum Replies Last Post
Output of the Statement Adrian Oracle Database 2 04-26-2008 04:40 AM
To get Output Rahulbatra C and C++ 1 05-10-2007 09:23 AM
Control Output caradoc DB2 3 04-09-2007 08:59 AM
Reason for Output cyrus C and C++ 1 01-14-2007 11:02 AM
C Programming - Managing Input and Output Operations JobHelper Career Advice 0 04-15-2006 08:30 AM


All times are GMT -4. The time now is 04:33 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2004 - 2007 Exforsys Inc. All rights reserved.