#define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<windows.h> #include<string.h> #include<stdlib.h> #include<math.h> typedef struct character_name { char name[100]; struct character_name* next; }char_name; typedef struct character_title { char title[100]; struct character_title* next; }char_title; typedef struct character_identity { char identity[100]; struct character_identity* next; }char_iden; typedef struct character_profession { char profession[100]; int level; struct character_profession* next; }char_pro; typedef struct character_ability { char ability[100]; char explain[1000]; struct character_ability* next; }char_abi; typedef struct character_race { char race[100]; struct character_race* next; }char_race; typedef struct character_information { int age; int gender; char_name* _name; char_title* _title; char_iden* _iden; char_pro* _pro; char_abi* _abi; char_race* _race; struct character_information* next; }char_inf; char_inf* initialization() { char_inf* node = (char_inf*)malloc(sizeof(char_inf)); node->_name = (char_name*)malloc(sizeof(char_name)); node->_title = (char_title*)malloc(sizeof(char_title)); node->_iden = (char_iden*)malloc(sizeof(char_iden)); node->_pro = (char_pro*)malloc(sizeof(char_pro)); node->_abi = (char_abi*)malloc(sizeof(char_abi)); node->_race = (char_race*)malloc(sizeof(char_race)); return node; } char_inf* ceshi, * current, * end; char_name* name_current, * name_end; char_title* title_current, * title_end; char_iden* iden_current, * iden_end; char_pro* pro_current, * pro_end; char_abi* abi_current, * abi_end; char_race* race_current, * race_end; int main() { ceshi = initialization(); ceshi->age = 666; ceshi->gender = 1; name_current = ceshi->_name; strcpy(name_current->name, "ceshi_name_1"); name_current->next = (char_name*)malloc(sizeof(char_name)); name_current = name_current->next; name_current->next = NULL; strcpy(name_current->name, "ceshi_name_2"); strcpy(ceshi->_title->title, "ceshi_title"); strcpy(ceshi->_iden->identity, "ceshi_identity"); strcpy(ceshi->_pro->profession, "ceshi_profession"); strcpy(ceshi->_abi->ability, "ceshi_ability"); strcpy(ceshi->_abi->explain, "ceshi_ability_explain"); strcpy(ceshi->_race->race, "ceshi_race"); ceshi->_pro->level = 666; name_current = ceshi->_name; printf(" Age: %d\n", ceshi->age); printf(" Gender: "); if (ceshi->gender == 0) printf("woman\n"); else printf("male\n"); while (name_current != NULL) { printf(" Name: %s\n", name_current->name); name_current = name_current->next; } printf(" Title: %s\n Identity: %s\n Profession: %d\n Ability: %s\n Ability_Explain: %s\n Race: %s\n", ceshi->_title->title, ceshi->_iden->identity,ceshi->_pro->level,ceshi->_abi->ability,ceshi->_abi->explain,ceshi->_race->race); return 0; } 怎样可以消除取消对NULL指针的引用这个警告
时间: 2024-04-14 07:27:09 浏览: 211
define _CRT_SEC.docx
要消除对NULL指针的引用警告,你可以使用以下方法:
1. 在编译时使用特定的编译选项或指令。例如,对于gcc编译器,你可以使用`-Wno-null-dereference`选项来禁用对NULL指针引用的警告。
```bash
gcc -Wno-null-dereference your_code.c
```
2. 使用编译指令来取消警告。在代码中,你可以使用特定的编译指令来取消对NULL指针引用的警告。在C语言中,你可以使用`#pragma`指令来实现。例如:
```c
#pragma GCC diagnostic ignored "-Wnull-dereference"
// 在这里写你的代码
#pragma GCC diagnostic warning "-Wnull-dereference"
```
这将在特定代码块中取消和重新启用对NULL指针引用的警告。
3. 针对特定行的警告,你可以使用条件语句来避免对NULL指针进行操作。例如,使用`if`语句检查指针是否为空。
```c
if (pointer != NULL) {
// 在这里进行指针操作
}
```
请注意,取消对NULL指针引用的警告可能会隐藏真正的问题,因此请谨慎使用此功能,并确保你了解代码中的潜在问题。
阅读全文