C Programming - Pointers - Discussion

Discussion Forum : Pointers - Find Output of Program (Q.No. 21)
21.
What will be the output of the program ?
#include<stdio.h>
#include<string.h>

int main()
{
    int i, n;
    char *x="Alice";
    n = strlen(x);
    *x = x[n];
    for(i=0; i<=n; i++)
    {
        printf("%s ", x);
        x++;
    }
    printf("\n", x);
    return 0;
}
Alice
ecilA
Alice lice ice ce e
lice ice ce e
Answer: Option
Explanation:

If you compile and execute this program in windows platform with Turbo C, it will give "lice ice ce e".

It may give different output in other platforms (depends upon compiler and machine). The online C compiler given in this site will give the Option C as output (it runs on Linux platform).

Discussion:
59 comments Page 1 of 6.

Garima Mahey said:   7 years ago
@All.

int i, n;
char *x="Alice";
n = strlen(x); // n=5
*x = x[n]; //*x='\0'(the base adress' element will be replaced by null character)
for(i=0; i<=n; i++)
{
printf("%s ", x); //in 1st iteration,it will print '\0lice' then 'ice' and so on..
x++;
}
printf("\n", x);
return 0;

Output :lice ice ce e
(4)

Bhup said:   4 years ago
Yes, it's a Segmentation fault (core dumped).
(2)

TDas said:   5 years ago
The 5th character of the string is '\0',therefore *x=*(x[0])==x[5]='\0'.Now the string x become x="lice".

For every x++ it will leave one character behind and print the rest for n=5 times. So, the output will be alice lice ice ce e.
(1)

Dake priyanka said:   6 years ago
In gcc platform shows an segmentation fault, why? I don't understand can you explain anyone of these.
(1)

Okayi said:   6 years ago
We cannot change 'A' to '\0'. Since "Alice" is stored in read-only shared memory. So 'A' will not get replaced.

Only we can make the pointer to point something else,
x = x + n //can be done.
*x = x[n] //cannot be done. //gives error.
(1)

Rajesh said:   1 decade ago
n=strlen(x);
gives n=5
*x=x[5]; can be written as *(x+0)=x[5]
and
x[5] contains '/0' so 'A' will replcd by '/0' since

Now our string will be like this:
x[0]='\0'
x[1]='l'
x[2]='i'
x[3]='c'
x[4]='e'
x[5]='\0'

Now look carefully :-

string become x[]={'\0','l','i','c','e','\0'};
for loop will be execute 6 times from i=0 to i=5

1st : at first position it find end char ('\0') so it will print nothing and x is incremented by 1 byte
2nd : x => l so it will print rest of string "lice" ,x++
3rd: x=>i so print "ice" and x++
4th: x=> c so print "ce" and x++
5th: x=> e so print "e" and x++
6th:x=> '\0' so nothing will be printed
at last printf("%s",x) will print nothing bz x =>'\0'
so result will be => lice ice ce e
(1)

Priya said:   10 years ago
Am getting option C while compiling. Please help me with right answer.

Selvakumar N said:   9 years ago
The question itself wrong or you need to add one more option for answer as "Undefined behaviour".

As I said above, In Linux, it throws "Segmentation fault" because of "*x = x[n];". It is correct since we are trying to change the string stored in read only memory. So what we have to do is create the memory which is not read only (In heap or in stack frame) as shown below

Solution: To avoid "Segmentation fault"

1.In heap:
char *x = (char *)malloc(10*sizeof(char));
strcpy(x,"Alice"); // copy in allocated mem
n = strlen(x);
*x = x[n];

2. In stack frame: Create char array & make the pointer to point the char array.
char *x;
char s[]="Alice";
x = s;
n = strlen(x);
*x = x[n];

Selva said:   9 years ago
In Linux, it throws "Segmentation fault" because of "*x = x[n];". It is correct since we are trying to change string constant.

Sandhya said:   9 years ago
Guys tell me in a simple way.


Post your comments here:

Your comments will be displayed after verification.