简体   繁体   English

为什么我的程序没有显示任何 output? 0 个错误,0 个警告但没有 output? 我正在使用 Dev C++ 编译器

[英]Why is my program not showing any output? 0 errors, 0 warnings but no output? I'm using Dev C++ compiler

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

struct node{
    int data;
    struct node *link;
};
void CountNodes(struct node *head); 

Is this declaration correct?这个声明正确吗? I really don't understand why it's not showing output.我真的不明白为什么它没有显示 output。

int main()
{
    struct node *head=(struct node *)malloc(sizeof(struct node));
    head->data=45;
    struct node *current=(struct node *)malloc(sizeof(struct node));
    current->data=90;
    head->link=current;
    struct node *next=(struct node *)malloc(sizeof(struct node));
    next->data=100;
    current->link=next;
    CountNodes(head);
}
void CountNodes(struct node *head)

Even this function is accurate.甚至这个 function 也是准确的。 It was working just 10 min ago but now it's not. 10 分钟前它还在工作,但现在不行了。

{
    int count=0;
    if(head==NULL)
    {
    printf("Linked list is empty!");
    }
    struct node *ptr=NULL;
    ptr=head;
    while(ptr!=NULL)
    {
        count++;
        ptr=ptr->link;
    }`enter code here`
    printf("%d",count);
}

Ok, I think I have found the bug in your code.好的,我想我已经在您的代码中找到了错误。


    struct node *head=(struct node *)malloc(sizeof(struct node));
    head->data=45;
    struct node *current=(struct node *)malloc(sizeof(struct node));
    current->data=90;
    head->link=current;
    struct node *next=(struct node *)malloc(sizeof(struct node));
    next->data=100;
    current->link=next;
    CountNodes(head);

It is your code in main function.这是您在主 function 中的代码。 Before answering the question here are some tips for you->在回答问题之前,这里有一些提示给你->

  1. Use tab Indentation to improve the readability of code.使用 tab 缩进来提高代码的可读性。
  2. In your code when you do current->data=90;在你的代码中当你做current->data=90; or assign a value to the struct then you should also assign the link pointer to NULL by current->link = NULL and this is why there is a bug in your code.或为结构分配一个值,那么您还应该通过current->link = NULLlink指针分配给NULL ,这就是代码中存在错误的原因。

When I ran your code on my system then it ran in an infinite loop.当我在我的系统上运行您的代码时,它会在无限循环中运行。 So the upgraded code for you program is ->所以你程序的升级代码是 ->

    struct node *head=(struct node *)malloc(sizeof(struct node));
    head->data=45;
    head->link = NULL;

    struct node *current=(struct node *)malloc(sizeof(struct node));
    current->data=90;
    current->link = NULL;
    head->link=current;

    struct node *next=(struct node *)malloc(sizeof(struct node));
    next->data=100;
    current->link=next;
    next->link = NULL;

    CountNodes(head);

In the above code after each assigning of value, I'm assign the link pointer to NULL which is a good practice and the bug was this too.在上面的代码中,每次赋值后,我将链接指针分配给NULL ,这是一个很好的做法,错误也是如此。

struct node *next=(struct node *)malloc(sizeof(struct node));
next->data=100;
current->link=next;
CountNodes(head);

Here above you have not initialized next pointer with anything.在上面你没有用任何东西初始化next指针。 So it is containing some Garbage Address and this running a loop that never ends.所以它包含一些垃圾地址,并且运行一个永无止境的循环。

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

相关问题 为什么这个简单的 C 程序没有显示任何输出? - Why is this simple C program not showing any output? 为什么 %d 在我的 c 程序 output 中显示 0? - Why %d is showing 0 in my c program output? 我正在使用turbo C ++,我的程序没有产生输出 - I am using turbo C++ and my program is producing no output 我的 C 程序是正确的,没有错误或警告,但没有显示 window。 为什么? - My C program is correct, with no errors or warnings, but no window is displayed. Why? 为什么我的程序不显示任何输出? - Why is my program not displaying any output? 使用Turbo C ++ 3.0编译器的C语言程序的输出令人困惑 - Perplexing Output of C language program using Turbo C++ 3.0 compiler 为什么我在 makefile 中指定的编译器标志没有出现在我的终端(Cygwin)output 中? - Why aren't compiler flags I specify in my makefile showing up in my terminal (Cygwin) output? 没有错误或警告,但我的代码输出是源源不断的 - No errors or warnings but my code output is an endless stream 如何退出字符计数程序(我正在使用dev c ++) - How to exit the character counting program (I'm using dev c++) 为什么我的程序 output 使用两次参数([-d?] 和 [-d|--data])? (我正在使用 popt 库进行选项解析) - Why does my program output an argument twice in usage ([-d?] and [-d|--data])? (i'm using popt library for options parsing)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM