我试图找出一个结构真的'是'并遇到问题,所以我真的有两个问题:
1)'sara'中保存了什么?它是指向结构的第一个元素的指针吗?
2)更有趣的问题:为什么不编译?GCC说"test.c:10:错误:分配中的不兼容类型",我无法弄清楚为什么......(这部分已经通过你的答案解决了,太棒了!)
#include <stdio.h>
struct name {
char first[20];
char last[20];
};
int main() {
struct name sara;
sara.first = "Sara";
sara.last = "Black";
printf("struct direct: %x\n",sara);
printf("struct deref: %x\t%s\n", *sara, *sara);
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
嗨,我正在使用C,我有一个关于分配指针的问题.
struct foo
{
int _bar;
char * _car[SOME_NUMBER]; // this is meant to be an array of char * so that it can hold pointers to names of cars
}
int foofunc (void * arg)
{
int bar;
char * car[SOME_NUMBER];
struct foo * thing = (struct foo *) arg;
bar = thing->_bar; // this works fine
car = thing->_car; // this gives compiler errors of incompatible types in assignment
}
Run Code Online (Sandbox Code Playgroud)
car和_car有相同的声明,为什么我收到有关不兼容类型的错误?我的猜测是它与指针有关(因为它们是指向char*数组的指针,对吧?)但我不明白为什么这是一个问题.
当我宣布char * car;而不是char * …