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.
Sundar said:
1 decade ago
Yes. *pt='\0'; (null char at end of string) should be added.
I tried without adding *pt='\0'; and I got the following output with junk values.
C:\TURBO>sample.exe
IndiaBIX♦¶«¿ç¤
After adding the *pt='\0'; I got the following output:
C:\TURBO>sample.exe
IndiaBIX
Hope this will help you. Have a nice day!
I tried without adding *pt='\0'; and I got the following output with junk values.
C:\TURBO>sample.exe
IndiaBIX♦¶«¿ç¤
After adding the *pt='\0'; I got the following output:
C:\TURBO>sample.exe
IndiaBIX
Hope this will help you. Have a nice day!
(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.
Dhiraj said:
1 decade ago
pt=t;
So here pt having the same address as t, but in order to treat as a string pt must be null terminated so for we are putting
*pt='\0'; explicitly. so that it can print IndiaBIX...
So here pt having the same address as t, but in order to treat as a string pt must be null terminated so for we are putting
*pt='\0'; explicitly. so that it can print IndiaBIX...
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?
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.
Madhureddy said:
1 decade ago
Every charecter array will be terminated by '\0'.
Hence we should check the end of char array with '\0'.
Hence the answer.
Hence we should check the end of char array with '\0'.
Hence the answer.
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)
Shwetha said:
1 decade ago
When I inserted the statement pt=ps; I got the output as IndiaBix can somebody explain this.
Shwetha said:
1 decade ago
I inserted the statement pt=ps; and I got the output as IndiaBix.
Please explain this.
Please explain this.
Alyka said:
1 decade ago
*pt contains the memory address for t & pt contains the simple value of t.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers