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.

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)

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];

Vasuroshan said:   1 decade ago
I will explain u guys for linux platform for this the answer is option 'c'.
first see this prog...Actually it is like above prog but with more abstract
#include<string.h>

int main()
{
int i, n;
char *x="Alice";
n = strlen(x);
printf("%d\n",n);
*x = x[n];
for(i=0; i<=n; i++)
{
printf("%s ", x);
printf(" ");
x++;
}
printf("\n", x);
return 0;
}

for this o/p is5
Alice lice ice ce e
so first printf in for loop it prints Alice next space shows next iteration and x++ shows lice and x++ then lice and loop continues and atlast x is e.....
hope u understand ....
sorry for my english...

CODIER said:   8 years ago
Actual code should be this, otherwise, an internal error will occur.

#include<stdio.h>
#include<string.h>
int main()
{
int i, n;
char *x = (char *)malloc(10*sizeof(char));
strcpy(x,"Alice");
n = strlen(x);//value of the n is 5
*x = x[n];//it copied the x[5] value('\0') to its base location so now string becomes " lice"
for(i=0; i<=n; i++)
{
printf("%s ", x);
x++;
}
printf("\n", x);
return 0;
}
now for i =0 printf will print from base location lets say 2000->" lice"
now for i=1 printf will print from 2001 upto null occur and so on->" lice ice ce e.

MUNCHUN PATHAK said:   9 years ago
#include<stdio.h>
#include<string.h>

int main()
{
int i, n;
char *x="Alice";
n = strlen(x);
// *x = x[n];// this line introduce run time error, if you run on linux plateform.without this output will be{Alice lice ice ce e};
for(i=0; i<=n; i++)
{
printf("%s ", x);
x++;
}
printf("\n", x);
return 0;
}

o/p={Alice lice ice ce e};

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)

Rajesh said:   1 decade ago
See dude *x=*x[] one and only same for char type declaration
here string length is five at the begining .
now intially *x=x[5]; so x represents the base address of array x[5] i.e x pointing to first element of array x[5] ie A , so its prints Alice ,next
x++ , so x is pointing to l , then it prints lice ,
at last x comes out of loop pointing to e , so it prints e.
thats it

Zodo said:   7 years ago
@ALL.

The code should be:

#include<stdio.h>
#include<string.h>

int main()
{
int i, n;
char x[20] = "Alice";
n = strlen(x);
char *const p=x;
*p = p[n];
char *q = p;
for(i=0; i<=n; i++)
{
printf("%s ", q);
q++;
}
printf("\n", p);
return 0;
}

Amit Wadhe 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'

So in 1st iteration i print \0lice
2nd : ice
3rd : ce
4th : e
5th : \0
i.e lice ice ce e

Souptick said:   1 decade ago
I run this program in Linux based system and output will be segmentation fault.

Cause x is pointing to string constant "Alice". So when we try to modify the string constant through x it is a illegal access and throw a runtime error.

*x = x[n]; This line is causing the error.


Post your comments here:

Your comments will be displayed after verification.