首页题目详情
定义一个结构体指针时,下列哪项是正确的?

A.struct Student *p = (Student *)malloc(sizeof(Student));
B.Student *p = malloc(sizeof(Student));
C.struct Student *p = (struct Student *)malloc(sizeof(Student));
D.struct Student *p = malloc(Student);
优质解答
答案
C
解析
在C语言中,malloc返回的是void指针,使用前需要强制转换。正确的做法是使用struct Student *p = (struct Student *)malloc(sizeof(Student));。选项A和B中的转换不正确,选项D语法错误。因此C选项正确。
理论知识部分单选题中等AI生成