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;
}
*pt='';
pt='\0';
pt='\n';
*pt='\0';
Answer: Option
Explanation:
No answer description is available. Let's discuss.
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!
(1)

Pavan said:   1 decade ago
What is difference between pt='\0' and *pt='\0' ?
(1)

Kirti said:   1 decade ago
But after execution I got IndiaBix without putting *pt='\0';

How is it. Can anyone explain it?
(1)

Siri said:   7 years ago
Well said @Bandhan.
(1)

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.

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...

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.

Alyka said:   1 decade ago
*pt contains the memory address for t & pt contains the simple value of t.


Post your comments here:

Your comments will be displayed after verification.