为结构变量定义字符串值
我刚刚回答了一些面试问题。遇到了与结构相关的问题,我不明白输出中发生了什么,请有人可以解释原因。
何时在结构中使用字符指针,例如
#include <stdio.h>
struct name {
char *array;
}variable;
int main( int argc, char * argv[] ){
variable.array="hello";
printf( "%s\n", variable.array );
}
打印输出 hello
,但是当将结构变量更改为 时,
struct name {
char array[10];
}variable;
编译器会抛出错误“赋值中的类型不兼容”,
variable.array="hello";
我真的很困惑我在哪里我没抓住要点。为什么会出现类似赋值问题的错误?请纠正我 谢谢
I was just going through certain interview questions. Got this structure related issue, I m not understanding what is happening wit the output, Please if some could explain the reason.
When is use a character pointer in the structure like
#include <stdio.h>
struct name {
char *array;
}variable;
int main( int argc, char * argv[] ){
variable.array="hello";
printf( "%s\n", variable.array );
}
The output is hello
printed, but when change the structure variable to
struct name {
char array[10];
}variable;
The compiler throws an error "incompatible types in assignment" at
variable.array="hello";
I am really confused as to where i am missing the point. Why does it show the error like assignment problem?? Please correct me
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您只能像在声明时那样初始化数组,
否则你需要使用
你甚至不能用一个简单的字符数组来做到这一点,
编译器会告诉你:
因为
"hello"
是一个字符串文字,它由一个不能被指针保存的字符串文字像这样分配给一个数组。You can only initialize array like that at the time of declaration only,
else you need to use
you can't even do like that with a simple char array
the complier will tell :
because
"hello"
is a string literal that is being held by a pointer which can't be assigned like this to an array.在 C 语言中,术语
"hello"
表示“指向包含字符 'hello\0
' 的字符串的指针”。因此,当您在第一种情况下分配
array="hello"
时,您是将指向字符串的指针分配给指针变量。这是正确的。在第二种情况下,您尝试将指针分配给数组,这是错误的!
你不能给数组赋值。
In C the term
"hello"
means "a pointer to a string that has the chars 'hello\0
' in it".So when you assign
array="hello"
in the first case you are assigning the pointer to the string into a pointer variable. Which is right.In the second case you try to assign a pointer to an array which is wrong!
you can't assign value to an array.
它会引发错误,因为 char 数组和指向 char 的指针是不同的东西。
数组不是指针。指针不是数组。有时指针可能指向数组。但即使指针指向数组,它也不是数组。
你的编译器在编译时给出了variable.array的地址。它还给出了字符串“hello”的地址。他们不是同一个地址。字符串文字和数组(或“缓冲区”)位于不同的位置;从逻辑上讲,必须将字符串复制到缓冲区中。
您可能会发现C 常见问题解答很有用。
It raises an error because an array of char and a pointer to char are different things.
An array is not a pointer. A pointer is not an array. Sometimes a pointer might point to an array. But even when a pointer points to an array, it's not an array.
Your compiler gives an address to variable.array at compile time. It also gives an address to the string literal "hello". They're not the same address. The string literal and the array (or "the buffer") are in different places; logically, the string has to be copied into the buffer.
You might find the C FAQ useful.
首先,数组是一个指向数组中第一个元素的常量指针
数组,一旦声明,我们就无法更改其地址位置..就像
The first thing is array is a constant pointer to the first element in the
array,we can not change its address location once we declared..like