gpt4 book ai didi

c - char *s= {'h' ,'e' ,'l' ,'l' ,'o' ,'\0' }; 的含义是什么?在 C 中它与 char *s ="hello"有何不同?

转载 作者:行者123 更新时间:2023-11-30 20:24:23 26 4
gpt4 key购买 nike

char *s={'h','e','l','l','o','\0'}; --- 给出警告:

warning: (near initialization for ‘s’)
warning: excess elements in scalar initializer

但是char *s="hello";通过在只读内存中分配 6 个字符的内存空间和指向该特定位置的 s 来显示 hello。

但是 char *s={'h','e','l','l','o','\0'}; 到底是什么?有何不同?

最佳答案

"hello" 这样的字符串文字会被视为指向字符的指针或字符数组,具体取决于您如何使用它。

char *s = "hello";    // treats the string literal as pointer-to-char
char s[] = "hello"; // treats the string literal as array-of-char

{'h','e','l','l','o','\0'} 这样的初始值设定项被视为数组。它不能分配给指针。

char *s = {'h','e','l','l','o','\0'};   // this is NOT valid
char s[] = {'h','e','l','l','o','\0'}; // same as char s[] = "hello"

关于c - char *s= {'h' ,'e' ,'l' ,'l' ,'o' ,'\0' }; 的含义是什么?在 C 中它与 char *s ="hello"有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33688372/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com