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;
}
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.
Ajay Yadav said:
6 years ago
In Linux environment answer will not be option C.
Because in Linux *x=x[n] ; this instruction will give you segmentation fault. Pointer can not be point to the memory location in such a way. So when you remove this instruction *x=x[n]; you will get answer Alice lice ice ce e.
Because in Linux *x=x[n] ; this instruction will give you segmentation fault. Pointer can not be point to the memory location in such a way. So when you remove this instruction *x=x[n]; you will get answer Alice lice ice ce e.
Abhishek said:
10 years ago
Hello guys, all the explanation is wrong regarding GCC.
Because here string is constant so we can't change the string content so here segmentation is dump because *x = x[5] of this.
We are trying to change a constant string which we can't do so output is segmentation fault.
Because here string is constant so we can't change the string content so here segmentation is dump because *x = x[5] of this.
We are trying to change a constant string which we can't do so output is segmentation fault.
Bunny said:
9 years ago
@Isaiah it is not due to null access.
In some compiler, it shows an error because x is a variable pointer to a constant string, so we can't change any character of the string because it's constant.
i.e *x=x[n]; its wrong.
If remove this line it will work fine.
In some compiler, it shows an error because x is a variable pointer to a constant string, so we can't change any character of the string because it's constant.
i.e *x=x[n]; its wrong.
If remove this line it will work fine.
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.
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)
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.
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)
Hotcpu said:
1 decade ago
I understand the answer D. However, how C is produced on the linux?
Based on the GNU C manual, when an array is initialized by a string, the '\0' will automatically add to the array. So, why in the online compiler x[5] is A instead of '\0'?
Based on the GNU C manual, when an array is initialized by a string, the '\0' will automatically add to the array. So, why in the online compiler x[5] is A instead of '\0'?
Rohit Jindal 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 be replaced by '/0' since
x[0] contains 'A'
x[1] contains 'l'
x[2] contains 'i'
x[3] contains 'c'
x[4] contains 'e'
x[5] contains '/0'
*x=x[5]; can be written as *(x+0)=x[5] and x[5] contains
'/0' so 'A' will be replaced by '/0' since
x[0] contains 'A'
x[1] contains 'l'
x[2] contains 'i'
x[3] contains 'c'
x[4] contains 'e'
x[5] contains '/0'
Ramalingeswara rao said:
8 years ago
remove
*x = x[n];
In this program, you will get option (c) as answer, otherwise it is segmentation fault in GCC
because above statement moves the pointer to null position i.e, after "e" in "Alice".
*x = x[n];
In this program, you will get option (c) as answer, otherwise it is segmentation fault in GCC
because above statement moves the pointer to null position i.e, after "e" in "Alice".
Vidyu said:
1 decade ago
char *x="Alice"; //means *x points to A
n=strlen(x); //n=5
*x=x[n]; //x[5]='\0' and *x points to A here we are assigning x[5] i.e \0 to to *x by this we are replacing A with \0.
Loop part is easy.
n=strlen(x); //n=5
*x=x[n]; //x[5]='\0' and *x points to A here we are assigning x[5] i.e \0 to to *x by this we are replacing A with \0.
Loop part is easy.
Rahul said:
1 decade ago
*x=A, n=5, so *x=x[5] will replace A with '\0' so new string will be. '\0lice so first iteration will not print anything because of \0 first. Next iteration lice will print.ice.ce.e will be printed.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers