Could someone please explain how this code works?

Here's the code :

void main()
{
int i,n;
char *x="Alice";
n=strlen(x);
*x=x[n];
for(i=0;i<=n;i++)
{
cout<<x<<" ";
}
cout<<endl<<x;
getch();
}


The output is :
lice ice ce e


Can you please explain what happens in every step?
Thanks! :D
Can you please explain what happens in every step?

Are you sure your program is working as intended?

lice ice ce e
I was told to find output for this program and then explain it to my class. I don't really understand how it works though. :/
Did you run the program?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int main()
{
int i,n;
char *x="Alice";
n=strlen(x);
*x=x[n];
for(i=0;i<=n;i++)
{
cout<<x<<" ";
}
cout<<endl<<x;
cin.get();
}


There is nothing in your program output.
Last edited on
If you remove *x=x[n];, the output will be like this :
Alice Alice Alice Alice Alice Alice Alice


http://cpp.sh/565kl
The output is :
lice ice ce e


I got this using turboc++
Topic archived. No new replies allowed.