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 3 of 6.
RAHUL JAIN said:
8 years ago
Segmentation fault in GCC Compiler.
Because x[5] is nothing it holding the value null.
So if we are printing value and increase the pointer address then code will go to segmentation fault in c.
Because x[5] is nothing it holding the value null.
So if we are printing value and increase the pointer address then code will go to segmentation fault in c.
Sumi naza said:
1 decade ago
In linux(GCC) it will give segmentation fault.
Because *x="alice" will store in code section n u can not change the data of code section because it "read only memory".
Because *x="alice" will store in code section n u can not change the data of code section because it "read only memory".
Shivam said:
1 decade ago
Given answer is right.
Because *a=a[n]....it assigns a null character at starting of string. So in first printf nothing will print then pointer moves ahead one by one. Hence the output.
Because *a=a[n]....it assigns a null character at starting of string. So in first printf nothing will print then pointer moves ahead one by one. Hence the output.
Ahmed Eldreny said:
2 years ago
It will give a segmentation fault because I tried to edit read-only data (char *x = "Alice"; this string is stored in read-only data).
So, anyone help me to get this clearly?
So, anyone help me to get this clearly?
Parikshit said:
1 decade ago
But at first iteration it is pointing to null value, so in first iteration it is space, & onword lice ice ce e so how it points to second character at second iteration.
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.
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.
Vinay said:
1 decade ago
Option D is compiler dependent, run this program on turbo c in windows and then then in linux you will understand.
Sakshi said:
1 decade ago
I dont understand that how the pointer after reaching the end gets back to print "lice" and so on...
Isaiah said:
10 years ago
1'st iteration printf get crashed due to null access. So need to add answer option with "system get crashed".
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers