小编Ehs*_*eri的帖子

为什么这个C代码不会改变字符串并导致缓冲区溢出?

我写了这段代码

#include <stdio.h>

int main()
{
    char String[] = "Hello the world!";
    char *pointer = String;
    int i;

    printf(" %s\n", pointer);

    pointer = "Helllooooo the worlldddddd";
    printf("Hello %s\n", pointer);
    printf("Hello %s\n", String);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

但我无法理解这条线是如何工作的.

pointer = "Helllooooo the worlldddddd";
Run Code Online (Sandbox Code Playgroud)

但我得到了这个输出

 Hello the world!
Hello Helllooooo the worlldddddd
Hello Hello the world!
Run Code Online (Sandbox Code Playgroud)

如您所见,它无法更改String值,但显示的字符数超过原始字符数.这不应该导致缓冲区溢出吗?不会破坏其他变量吗?

c string pointers

2
推荐指数
1
解决办法
85
查看次数

标签 统计

c ×1

pointers ×1

string ×1