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

Shwetha said:   1 decade ago
I inserted the statement pt=ps; and I got the output as IndiaBix.

Please explain this.

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.

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

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?


Post your comments here:

Your comments will be displayed after verification.