Change the input to the above sentence (you did not address the variable of type int)
After correction, there will be no memory error (no crash)
But the program will fall into an infinite loop.
Simply analyze: your point e is a parameter of the function QueueTraverse and has not been initialized.
Is this the problem? I don't know the purpose of writing this program, so I don't know the specific details.
The following is the complete process:
// testing2.cpp: defines the entry point of the console application.
//
# include < lt string.h >//string function header file
# include & ltctype.h & gt// Character function header file
# include & ltmalloc.h & gt// malloc (), etc.
# include & lt restrictions. & gt// INT_MAX, etc.
# include & ltstdio.h & gt// standard input/output header file
# include & ltstdlib.h & gt// atoi(),exit()
# include & ltio.h & gt// eof()
# include & ltmath.h & gt// mathematical function header file
# Define true 1
# Define false 0
# Define OK 1
# Definition error 0
Typedef int status; // Status is the type of function, and its value is the function result status code.
#define MAX_QSIZE 100 // Maximum queue length+1
# Define N 2
typedef int QElemType
Typedef structure
{
char name[20];
char id[20];
int x;
int y;
int z;
} point;
Typedef structure
{point * base// Initializes dynamically allocated storage space.
Int front// Head pointer, which points to the queue head element if the queue is not empty.
Int rear// tail pointer, if the queue is not empty, it points to the next position of the queue tail element.
} SqQueue
void init queue(SqQueue & amp; Q)
{//Construct an empty queue q
q . base =(point *)malloc(MAX _ QSIZE * size of(point));
If (! Q.base) // Storage allocation failed.
Exit (overflow);
q . front = q . rear = 0;
}
Empty status queue (SqQueue Q)
{//Returns TRUE if the queue q is empty; Otherwise return FALSE.
If(Q.front==Q.rear) // Empty queue flag
Return TRUE
other
Returns FALSE
}
State Queue (SqQueue & ampq, point E)
{//Insert a new tail element whose element E is the queue Q..
If ((q.rear+1)% max _ qsize = = q.front)//The queue is full.
Error returned;
q . base[q . rear]= e; //Insert e at the end of the line
q . rear =(q . rear+ 1)% MAX _ QSIZE; //After the tail pointer+1, take the remainder of MAX_QSIZE.
Return ok;
}
int QueueLength(SqQueue Q)
{//Returns the number q of elements in the queue, that is, the length of the queue.
return(q . rear-q . front+MAX _ QSIZE)% MAX _ QSIZE;
}
Int dequeue (SqQueue & ampq point & ampe)
{//If the queue Q is not empty, delete the queue head element of Q, use E to return its value, and return OK; Otherwise, an error is returned.
If(Q.front==Q.rear) // The queue is empty.
Error returned;
e = q . base[q . front]; //Assign the value of the queue header element to e..
q . front =(q . front+ 1)% MAX _ QSIZE; //Move the group leader pointer
Return ok;
}
void QueueTraverse(SqQueue Q)
{//Output each element in the queue Q from the head to the end of the queue.
Int I = Q.front// I originally pointed to the element of team leadership.
Point e = q.base [I]; //Assign the element pointed by I to E.
And (me! =Q.rear) // i points to an element in the queue q.
{
printf(" name:% s id = % s x = % d y = % d z = % d \ n ",e.name,e.id,e.x,e.y,e . z); //output the element indicated by i.
I =(I+ 1)% MAX _ QSIZE; // i points to the next element.
}
printf(" \ n ");
}
int main()
{
int I;
SqQueue Q;
init queue(Q);
Point p [n];
for(I = 1; I < = n; i++)
{
Printf ("Enter the name id x y z of %dth point", i);
scanf("%s %s %d %d %d ",p[i]。 Name, p[i]. id & amp; p[i]。 x & amp; p[i]。 y & amp; p[i]。 z);
EnQueue(Q,p[ 1]);
}
queue traverse(Q);
System ("suspended");
Returns 0;
}