The output of this code is "lice ice ce e". Seems to be string array is acting like a circular array. But how is it possible ????

#include<stdio.h>
#include<conio.h>

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

Recommended Answers

All 2 Replies

%s prints the whole string. If the starting point for the string steps ahead by one character each iteration, you basically chop off the first character each time.

char const* x = "Alice";

printf("%s", x); // "Alice"
++x;
printf("%s", x); // "lice"
++x;
printf("%s", x); // "ice"
++x;
printf("%s", x); // "ice"
++x;
printf("%s", x); // "ce"
++x;
printf("%s", x); // "e"

By the way, this line:

*x=x[n];

is very bad. x is a pointer to a string literal, and string literals are not allowed to be modified. But that statement tries to overwrite the first character of the string literal with the last.

deceptikon Thanks.......... got it now ......... Thread Solved...........

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.