简体   繁体   English

将字符串文字分配给数组

[英]Assign to a string literal an array

It is undefined behavior to do this? 这样做是不明确的行为?

char *a = "hello";
char b[] = "abcd";
a = b;

My compiler throws no warning, with the maximum warning level. 我的编译器没有抛出任何警告,具有最大警告级别。

There is no UB here. 这里没有UB。 You are simply re-assigning a pointer to point at the address of the start of the array instead. 您只需重新指定一个指针指向数组开头的地址。

Note that you are not actually modifying the value that a is pointing at, only a itself, and a is a normal char * . 请注意,您实际上并没有修改该值a所指向,只有a本身, a是正常char *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM