C、指针、字符串

发布于 2024-11-05 08:18:32 字数 245 浏览 0 评论 0原文

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

输出是什么?
请解释输出......................
o/p 是:

irl
rl
l
main()
{
  char *x="girl";
  int n,i;
   n=strlen(x);
  *x=x[n];
  for(i=0;i<n;i++)
  {
   printf("%s \n",x);
    x++;
  }
}

What is the output?
Please explain the output.......................
o/p is :

irl
rl
l

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幻想少年梦 2024-11-12 08:18:32

输出是未定义的行为。您修改了字符串文字。

The output is undefined behaviour. You modified a string literal.

生生不灭 2024-11-12 08:18:32

正如其他人指出的那样,编写的程序具有未定义的行为。

如果你做了一个小改动:

char x[] = "girl";

那么我相信这是合法的,并且可以解释。 (编辑:实际上它仍然存在问题。它是 int main(),你应该在最后 return 0; 。你还需要 #include 因为您使用的是 strlen#include 因为您使用的是 printf >.)

该行将

*x = x[n];

x[0] (即 *x)设置为 x[4] (恰好是字符串终止符 <代码>'\0')。因此,要打印的第一个字符串是空字符串,因为第一个字符是字符串终止符。

然后我们循环遍历字符串,一次一个字符,打印子字符串:

irl
rl
l

As others have pointed out, the program as written has undefined behaviour.

If you make a small alteration:

char x[] = "girl";

then I believe it is legal, and possible to explain. (EDIT: Actually there are still problems with it. It's int main(), and you should return 0; at the end. You also need to #include <string.h> because you are using strlen, and #include <stdio.h> because you are using printf.)

The line

*x = x[n];

sets x[0] (i.e. *x) to x[4] (which happens to be the string terminator '\0'). Thus the first string to be printed is the empty string, because the very first character is the string terminator.

We then loop through the string, one character at a time, printing the substrings:

irl
rl
l
恋你朝朝暮暮 2024-11-12 08:18:32

虽然结果是未定义的行为,正如 DeadMG 所说,但我们假设您将 x 声明为 char x[] = "girl"。

您将 4 分配给 n(因为单词“girl”的长度为 4),并将 x[4] 中的值分配给 *x< /code> (即 x[0]),但该值为 '\0'(空终止符)

现在循环并打印从 x 到下一个空终止符的单词,但这是第一次,第一个字符是空终止符,所以你得到 没有什么。之后,您可以从递增索引中打印单词。

g   i    r   l   \0

*x = x[4]:

\0   i    r    l   \0
^    ^    ^    ^
it1  it2  it3  it4      // << Where does x points to in each iteration of the for loop

While the result is undefined behavior, as DeadMG said, let's assume you declared x as char x[] = "girl".

You assign 4 to n (since the length of the word "girl" is 4), and you assign the value in x[4] to *x (which is x[0]), but this value is '\0' (null terminator)

Now you loop and print the word from x to the next null terminator, but the first time, the first char is the null terminator, so you get nothing. after that you print the word from incrementing index.

g   i    r   l   \0

*x = x[4]:

\0   i    r    l   \0
^    ^    ^    ^
it1  it2  it3  it4      // << Where does x points to in each iteration of the for loop
菊凝晚露 2024-11-12 08:18:32

该代码非常可疑。 *x=x[n] 尝试覆盖文字“girl”,效果会因平台和编译器而异。更正确的是,它应该声明为:

const char *x = "girl";

然后它不会(不应该)编译。

The code is distictly suspect. *x=x[n] attempts to overwrite the literal "girl", and the effect will vary between platforms and compilers. More correctly it should be declared as:

const char *x = "girl";

and then it will not (should not) compile.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文