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.
Aishwarya said:
1 decade ago
Since pt is a character array the string is finally added a '\0'.
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.
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...
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)
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.
Sundar said:
1 decade ago
@Wikiok
Thanks for your explanation.
Thanks for your explanation.
Pavan said:
1 decade ago
What is difference between pt='\0' and *pt='\0' ?
(1)
Alyka said:
1 decade ago
*pt contains the memory address for t & pt contains the simple value of t.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers