面试题答案
一键面试#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义结构体
typedef struct {
int num1;
int num2;
} MyStruct;
int main() {
MyStruct *ptr;
// 使用malloc为结构体分配动态内存
ptr = (MyStruct *)malloc(sizeof(MyStruct));
if (ptr == NULL) {
// malloc分配内存失败,打印错误信息
fprintf(stderr, "内存分配失败: %s\n", strerror(errno));
return 1;
}
// 使用完后释放内存
free(ptr);
return 0;
}