简体   繁体   English

退出后C程序崩溃

[英]C program crash after exit

i'm a total beginner at c Programming. 我是c编程的初学者。 So i might not know alot of stuffs. 所以我可能不知道很多东西。 But could anyone tell me why is my code crashing as soon as it is done with the last line? 但是谁能告诉我为什么我的代码在最后一行完成后就崩溃了? could it be the arrays that i defined? 难道是我定义的数组?

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


void main()
{

unsigned int totSession, totStudent;
int x, y, i, j;
int *studID,*con,*k;
char (*name1)[40];
int *frequency;

printf("Enter number of consultation\n"); //input number of consultation
scanf("%d", &totSession);
printf("Enter number of students\n"); //input number of students
scanf("%d", &totStudent);

studID = (int *)malloc(totStudent * sizeof(int));
con = (int *)malloc(totStudent * sizeof(int)); 
name1 = (char *)malloc(totStudent * sizeof(char));
frequency = (int *)malloc(totStudent * sizeof(int));
k = (int *)malloc(totSession * sizeof(int));

for (x = 0; x < totStudent; ++x) //Entering each students details
{
    printf("Enter details of Student[%d]\n", x + 1);
    printf("Enter StudentID:");
    scanf("%d", &studID[x]);
    printf("Enter student's Full name:");
    scanf("%s", &name1[x]);
    printf("Which consultation to choose for student:");
    scanf("%d", &con[x]);
}

printf("%s%15s%40s\n", "Student ID", "Fullname", "Which consultation session to choose");

for (y = 0; y < totStudent; ++y) //print all student details
{
    printf("%-17d%-23s%d\n\n", studID[y],name1[y],con[y]);
}


for (i = 0; i <= totStudent; ++i) //set all arrays of frequency to 0
{
    frequency[i]=0;
    k[i] = 0;
}

for (i = 0; i < totStudent; ++i) //summarise consultation
{
    ++frequency[con[i]];
    k[i + 1] += (i+1);           //create constant for session number
}

if (totStudent > 1)
{
    int hold, hold1;
    for (i = 0; i < totSession; ++i) //sorting
    {
        for (j = 1; j <= (totSession - 1); ++j)
        {

            if (frequency[j] < frequency[j + 1])
            {
                hold = frequency[j];
                hold1 = k[j];
                frequency[j] = frequency[j + 1];
                k[j] = k[j + 1];
                frequency[j + 1] = hold;
                k[j + 1] = hold1;
            }
        }
    }
}
printf("%s%50s", "Consultation Session Number", "The number of students choose this session\n");

for (i = 1; i <= totSession; ++i)
{
    printf("%10d%40d\n",k[i], frequency[i]);
}
printf("The session to offer is: Session %d with %d (out of %d) students chosen.\n", k[1], frequency[1], totStudent);


getch();
}

any help would be appreciated!! 任何帮助,将不胜感激!! have done free() at the end of the codes, but it doesnt work too.. 已经在代码末尾做了free(),但是它也不起作用。

If by "crashing" you mean it fails to compile the source with the following error: 如果“崩溃”是指它无法编译源,并出现以下错误:

/tmp/ccvGANfB.o: In function `main':
tmp.c:(.text+0x4d2): undefined reference to `getch'
collect2: error: ld returned 1 exit status

it's because you are trying to use a function (getch) that doesn't exists (to be fair it exists inside the curses library, but I highly doubt that's what you mean to use). 这是因为您正在尝试使用一个不存在的函数(提取)(公平地说,它存在于curses库中,但是我非常怀疑那是您的意思)。 You probably missplelled getchar(). 您可能会错用getchar()。

In short, in the last line substitute 简而言之,在最后一行替代

 getch();

with

 getchar();

PS: by "crashing" one usually refers to a run-time error (the program fails while executing ; your program does not compile, it throws a compile time error. PS:“崩溃”通常是指运行时错误(程序在执行时失败;您的程序无法编译,会引发编译时错误。

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

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