C Programming - Pointers - Discussion
Discussion Forum : Pointers - Point Out Correct Statements (Q.No. 7)
7.
Which statement will you add to the following program to ensure that the program outputs "IndiaBIX" on execution?
#include<stdio.h>
int main()
{
char s[] = "IndiaBIX";
char t[25];
char *ps, *pt;
ps = s;
pt = t;
while(*ps)
*pt++ = *ps++;
/* Add a statement here */
printf("%s\n", t);
return 0;
}
Discussion:
14 comments Page 1 of 2.
Gayathri said:
6 years ago
In my point of view, While applying operator precedence. It doesn't assign the first letter of the string 'I' to t; Can anyone clarify my doubt?
Siri said:
7 years ago
Well said @Bandhan.
(1)
Bandna said:
1 decade ago
@Shwetha.
As we have assigned pointers corresponding to s and t, while doing ps=pt, it simply copies the whole string s into t.
So you are getting output as "IndiaBix".
We can't do this with array as we can't write s=t.
As we have assigned pointers corresponding to s and t, while doing ps=pt, it simply copies the whole string s into t.
So you are getting output as "IndiaBix".
We can't do this with array as we can't write s=t.
Shwetha said:
1 decade ago
I inserted the statement pt=ps; and I got the output as IndiaBix.
Please explain this.
Please explain this.
Shwetha said:
1 decade ago
When I inserted the statement pt=ps; I got the output as IndiaBix can somebody explain this.
Kirti said:
1 decade ago
But after execution I got IndiaBix without putting *pt='\0';
How is it. Can anyone explain it?
How is it. Can anyone explain it?
(1)
Alyka said:
1 decade ago
*pt contains the memory address for t & pt contains the simple value of t.
Pavan said:
1 decade ago
What is difference between pt='\0' and *pt='\0' ?
(1)
Sundar said:
1 decade ago
@Wikiok
Thanks for your explanation.
Thanks for your explanation.
Wikiok said:
1 decade ago
If *pt reach the '\0' element, the while statement goes false, so the last element ('\0') won't be copied. It has to be done manually.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers