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

Sahitya said:   8 years ago
I executed this and I got option c.

Balaji said:   8 years ago
In gcc compiler, this program is crashed because *x=x[5] (x[5] is not allocated memory ). Am i right?

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

Divyaprabha said:   9 years ago
Guys, please explain What is the last printf("\n", x) doing here?

Sumit said:   9 years ago
But it is pointer to string constant which cannot be modified if it can be modified in turboC then its correct otherwise its an error.

Sagar said:   9 years ago
Thanks for the explanation.

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.