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 1 of 6.
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?
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.
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.
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)
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.
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
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)
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;
}
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;
}
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".
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers