我的代码中什么都没有打印。没有编译器问题。为什么不打印任何内容?我也没有收到警告。
#include <stdio.h>
#include <string.h>
int main()
{
struct site
{
int no_of_pages;
char name[20];
};
struct site *ptr;
ptr->no_of_pages = 665;
printf("%d\n",ptr->no_of_pages);
char array[20];
strcpy(array, "hello");
strcpy(ptr->name, "Singularity");
printf("%s\n",ptr->name);
return 0;
}
答案 0 :(得分:2)
是的,有一个警告指出了问题的根源:
$ cc -Wall test.c
test.c:13:5: warning: variable 'ptr' is uninitialized when used here
[-Wuninitialized]
ptr->no_of_pages = 665;
^~~
答案 1 :(得分:2)
首先初始化指针
struct site *ptr = malloc(sizeof(struct site));