简体   繁体   English

没有错误,但是当我执行它时,它显示 a.exe 没有运行

[英]No errors but when i execute it, it is showing a.exe is not running

I made a queue ADT program, and it has no error, but when I run this code, it shows that a.exe has stopped working.我做了一个队列ADT程序,没有错误,但是当我运行这段代码时,它显示a.exe已经停止工作。 I use Windows 7 with i5 processor.我使用 Windows 7 和 i5 处理器。 Please explain the problem.请解释问题。

Here is the code:这是代码:

#include<stdio.h>
#include<stdlib.h>

typedef struct node_info
{
    void*dataptr;
    struct node_info*next;
}node;

typedef struct 
{
    node* f;
    node* r;
    int count;
}queue;

queue*createq()
{
    queue*qp;
    qp=(queue*)malloc(sizeof(queue));
    if(!qp)
        return NULL;
    qp->count=0;
    qp->f=NULL;
    qp->r=NULL;
    return qp;
}

int enq(queue*qp,void*data)
{
    node*temp;
    temp=(node*)malloc(sizeof(node));
    if(!temp)
        return 0;
    temp->dataptr=data;
    temp->next=NULL;
    if(qp->count==0)
        qp->f=temp;
    else
        qp->r->next=temp;
    qp->r=temp;
    (qp->count)++;
    return 1;
}

void*deq(queue*qp)
{
    if(qp->count==0)
        return 0;
   node*temp;
   void*dout;
   temp=qp->f;
   dout=temp->dataptr;
   qp->f=temp->next;
   if(qp->count==1)
        qp->r=NULL;
    (qp->count)--;
    return dout;
}

void displayq(queue*qp)
{
    node*temp=qp->f;
    if(qp->count==0)
        printf("queue is empty");
    else
    { 
        printf("%d",*(int*)temp->dataptr);
        temp=temp->next;
    }
}

int main (queue*qp)
{
    int ele,choice;
    printf("\nEnter\n1.enqueue\n2.dequeue\n3.display\n\n");
    scanf("%d",&choice);
    while(1)
    {
        switch(choice)
        {
            case 1: 
                printf("enter the element to insert\n");
                scanf("%d",&ele);
                if(enq(qp,&ele))
                printf("%d has been inserted",*(int*)qp->r->dataptr);
                else
                printf("element not inserted");
            case 2:
                if(qp->count==0)
                printf("queue is empty");
                printf("%d is removed",*(int*)(deq(qp)));
            case 3:
                displayq(qp);
        } 
    }
}

Here is a screenshot这是截图

Incorrect definition of main main的定义不正确

The compiler did emit meaningful warnings, but you decided to ignore them.编译器确实发出了有意义的警告,但您决定忽略它们。

Unless this declaration,除非这个声明,

int main(queue *qp)

is provided and documented by a certain implementation, it is invalid.由某个实现提供和记录,它是无效的。


From C11:来自 C11:

The function called at program startup is named main.在程序启动时调用的 function 被命名为 main。 The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:该实现没有为这个 function 声明原型。它应该被定义为返回类型 int 并且没有参数:

int main (void) { /* ... */ }

or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):或使用两个参数(此处称为 argc 和 argv,但可以使用任何名称,因为它们是声明它们的 function 的本地名称):

int main (int argc, char *argv[]) { /* ... */ }

or equivalent;或同等学历; or in some other implementation-defined manner.或者以其他一些实现定义的方式。


Always compile with warnings enabled, and consider warnings as errors.始终在启用警告的情况下进行编译,并将警告视为错误。


To cast or not to cast?投还是不投?

Regarding casting the result of malloc and family:关于铸造malloc和家庭的结果:

It is redundant and might hide a bug.它是多余的,可能隐藏了一个错误。 These functions returns a generic void pointer, or void * , that is automatically promoted to the correct type.这些函数返回一个通用的void指针或void * ,它会自动提升为正确的类型。


Fall-through:直通:

Every case in a switch requires a break statement or you risk fall-through. switch中的每个case都需要一个break语句,否则你就有失败的风险。 (Assuming fall-through is not intentional) (假设掉线不是故意的)

From C11:来自 C11:

6.8.6.3 The break statement 6.8.6.3 中断语句

Constraints约束条件

1 A break statement shall appear only in or as a switch body or loop body. 1 break 语句只能出现在开关体或循环体中或作为开关体或循环体出现。

Semantics语义学

2 A break statement terminates execution of the smallest enclosing switch or iteration statement. 2 break 语句终止最小封闭开关或迭代语句的执行。


scanf() returns a value: scanf()返回一个值:

scanf() returns the number of elements it has successfully processed and converted. scanf()返回它已成功处理和转换的元素数。 You should check if it succeeded.你应该检查它是否成功。

Aside: Space-bar makes for readability, use it.旁白:空格键有助于提高可读性,请使用它。 As is, your code is unreadable and it's hard to decipher where a potential bug might be.照原样,您的代码不可读,并且很难破译潜在错误的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM