0

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();
}
2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by ram619
Featured Replies
  • 1

    %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. [code] char const* x = "Alice"; printf("%s", x); // "Alice" ++x; printf("%s", x); // "lice" ++x; printf("%s", x); // "ice" ++x; printf("%s", … Read More

1

%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.

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.