Home > Back-end >  Cancel the NULL pointer references, for help you requested
Cancel the NULL pointer references, for help you requested

Time:10-05

Today learned a linked list, on the heap manual opening and assignment can run normally, but in the end tip warning: cancel the NULL pointer "node" on the reference, can't think of is where there is a problem, to help you, as a novice, please light spray
 # define _CRT_SECURE_NO_WARNINGS 
#include
#include
#include

Struct the Teacher
{
Char name [64].
int age;
Char sex [12].
Struct the Teacher * LinkNode;
};

Void distr ()
{
Struct the Teacher * node1=malloc (sizeof (struct the Teacher));
Struct the Teacher * 2=malloc (sizeof (struct the Teacher));
Struct the Teacher * node3=malloc (sizeof (struct the Teacher));
Struct the Teacher * node4=malloc (sizeof (struct the Teacher));
Struct the Teacher * node5=malloc (sizeof (struct the Teacher));
Struct the Teacher * node6=malloc (sizeof (struct the Teacher));

//manual for each teacher's attribute assignment
Node1 - & gt; Age=24; Strcpy (node1 - & gt; The name, "Mr Li"); Strcpy (node1 - & gt; Sex, "male");
2 - & gt; Age=41; Strcpy (2 - & gt; The name, "Mr Wu"); Strcpy (2 - & gt; Sex, "female");
Node3 - & gt; Age=32; Strcpy (node3 - & gt; The name "zhou"); Strcpy (node3 - & gt; Sex, "male");
Node4 - & gt; Age=34; Strcpy (node4 - & gt; The name, "miss huang"); Strcpy (node4 - & gt; Sex, "male");
Node5 - & gt; Age=28; Strcpy (node5 - & gt; The name, "wang"); Strcpy (node5 - & gt; Sex, "female");
Node6 - & gt; Age=33; Strcpy (node6 - & gt; The name, "xue teacher"); Strcpy (node6 - & gt; Sex, "female");

//build relationships in the list
Node1 - & gt; LinkNode=2;
2 - & gt; LinkNode=node3;
Node3 - & gt; LinkNode=node4;
Node4 - & gt; LinkNode=node5;
Node5 - & gt; LinkNode=node6;
Node6 - & gt; LinkNode=NULL;

//traversing and print the list
Struct the Teacher * Ptemp=node1;
While (Ptemp!=NULL)
{
The teacher's name is: printf (" % s \ t gender is: % s \ t age is: % d \ n ", Ptemp - & gt; The name, Ptemp - & gt; Sex, Ptemp - & gt; The age);
Ptemp=Ptemp - & gt; LinkNode;
}

//release list
Free (node6); Free (node5); Free (node4); Free (node3); Free (2); Free (node1);

}


Int main ()
{
Distr ();

system("pause");
Return EXIT_SUCCESS;
}

CodePudding user response:

Struct the Teacher * node1= (struct the Teacher *) malloc (sizeof (struct the Teacher));
Several other so are processing

No problem

CodePudding user response:

1/f, bosses, this method still not VS2019 or prompt the same warning,,, thank you very much for the reply of

CodePudding user response:

Put the warning posted, where the line? What is specific textual warning?

CodePudding user response:

 severity code shows the project file line prohibits display status details 
Warning C6011 cancel to NULL pointer "node1" reference, day06 C: \ USERS/ADMINISTRATOR/DESKTOP/one-way dynamic linked list. 25 C

CodePudding user response:

CodePudding user response:

Warning meaning is to let you check the pointer is NULL mean
You this is VS2019?
Under VS2019 strcpy become strcpy_s and suggest what time into such English description

If (node1!=NULL) {
Node - & gt; Age=...
}

CodePudding user response:

Or, unified before 25 line assignment write a line
If (NULL==node1 | | NULL==2 | |... ) return;

This test it is to let you to have a NULL after malloc judgment then do other processing
  • Related