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 2 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)
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...
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.
Aishwarya said:
1 decade ago
Since pt is a character array the string is finally added a '\0'.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers