struct student {
char name[30];
char rollNumber[20];
char class[2];
};
void test(int length){
struct student* tempStudent = (struct student*)malloc((length+1)*sizeof(struct student));
*tempStudent[0].name = "Hello";
printf("%s", *tempStudent[0].name);
}
void main(){
test(1);
}
当我尝试运行上面的代码时,我收到一条警告“警告:从‘char *’对‘char’的赋值使指针中的整数不进行强制转换”,并且输出为“(null)”。我做错了什么?
最佳答案
*tempStudent[0].name
与 tempStudent[0].name[0]
完全相同。 IE。它是一个 char
元素。
您需要将字符串复制到数组name
中:
strcpy(tempStudent[0].name, "Hello");
并且在打印时也使用数组本身:
printf("%s\n", tempStudent[0].name);
关于c - 我收到错误 "warning: assignment to ' char' from 'char *' made integer frompointer without a Cast”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66597142/