簡體   English   中英

你如何在C中正確分配空間

[英]How do you properly allocate space in C

我正在用C編寫一個程序,我知道你必須為有效的代碼分配適當的空間,但我不確定如何啟動,我已經編寫並完成了我的程序,重新啟動將是一種恥辱。 如果有人可以指導我如何為我的程序正確malloc空間,將不勝感激。 這是我的代碼如下:

main.c中

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "courses.h"

int main (int argc, char *argv[]) {
    srand (time(NULL));
    createStudents ();
    createCourses ();
    regiserStudents ();
    printCourses ();

    return 1;
}

courses.c

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include "structs.h"

void createStudents () {
  int random, i;

  for (i = 0; i < 12; i++) {
    students[i].firstName = *firstName[i];
    students[i].lastName = *lastName[i];
    random = 10000 + rand() % 89999;
    students[i].num.studentNum = random;
    printf("%d - %s, %s \n", students[i].num.studentNum, students[i].lastName, students[i].firstName);
  }
}

void createCourses () {
  int numbers[999];
  int numbersLeft = 999;
  char courseCode[512];
  int numCourses = 3;
  int random, i, j;

  for (i = 0; i < 999; i++) {
    numbers[i] = i;
  }

  for (j = 0; j < numCourses; j++) {
    random = rand() % numbersLeft;
    if (random < 10) {
      snprintf(courseCode, sizeof courseCode, "CS00%d", random);
    }
    else if (random < 100 && random > 9) {
      snprintf(courseCode, sizeof courseCode, "CS0%d", random);
    }
    else {
      snprintf(courseCode, sizeof courseCode, "CS%d", random);
    }

    courses[j].cName = courseName[j];
    courses[j].cDescription = courseDescription[j];
    courses[j].cCode = courseCode;
    numbers[random] = numbers[numbersLeft-1];
    numbersLeft--;

    random = 4 + rand() % 4;
    courses[j].maxRegister = random;
  }
}

void regiserStudents () {
  int checkSum = 0, checkSum1 = 0, checkTemp = 0, count0 = 0, count1 = 0, count2 = 0;
  int v, i, j, random;

  for (i = 0; i < 2; i++) {
    checkTemp = count0;

    for (j = 0; j < 12; j++) {
      random = rand() % 3;
      if (random == 0) {
        if (count0 == 0) {
          courses[random].registered[count0] = &students[j];
          count0++;
        }
        else {
          checkSum1 = students[j].num.studentNum;

          for (v = 0; v < checkTemp; v++) {
            checkSum = courses[0].registered[v]->num.studentNum;
            if (checkSum == checkSum1) {
              /*Do Nothing*/
            }
            else {
              courses[random].registered[count0] = &students[j];
              count0++;
            }
          }
        }
      }
      if (random == 1) {
        if (count1 == 0) {
          courses[random].registered[count1] = &students[j];
          count1++;
        }
        else {
          checkSum1 = students[j].num.studentNum;

          for (v = 0; v < checkTemp; v++) {
            checkSum = courses[1].registered[v]->num.studentNum;
            if (checkSum == checkSum1) {
              /*Do Nothing*/
            }
            else {
              courses[random].registered[count1] = &students[j];
              count1++;
            }
          }
        }
      }
      if (random == 2) {
        if (count2 == 0) {
          courses[random].registered[count2] = &students[j];
          count2++;
        }
        else {
          checkSum1 = students[j].num.studentNum;

          for (v = 0; v < checkTemp; v++) {
            checkSum = courses[2].registered[v]->num.studentNum;
            if (checkSum == checkSum1) {
              /*Do Nothing*/
            }
            else {
              courses[random].registered[count2] = &students[j];
              count2++;
            }
          }
        }
      }
    }
  }
  courses[0].studentRegistered = count0;
  courses[1].studentRegistered = count1;
  courses[2].studentRegistered = count2;
}

void printCourses () {
  int i;
  printf("\n%s - %s\n%s\nRegistered Students (%d/%d):\n", courses[0].cCode, courses[0].cName, courses[0].cDescription, courses[0].studentRegistered, courses[0].maxRegister);

  for (i = 0; i < courses[0].studentRegistered; i++) {
    printf("%d - %s, %s \n", courses[0].registered[i]->num.studentNum, courses[0].registered[i]->lastName, courses[0].registered[i]->firstName);
  }
  printf("\n%s - %s\n%s\nRegistered Students (%d/%d):\n", courses[1].cCode, courses[1].cName, courses[1].cDescription, courses[1].studentRegistered, courses[1].maxRegister);

  for (i = 0; i < courses[1].studentRegistered; i++) {
    printf("%d - %s, %s \n", courses[1].registered[i]->num.studentNum, courses[1].registered[i]->lastName, courses[1].registered[i]->firstName);
  }
  printf("\n%s - %s\n%s\nRegistered Students (%d/%d):\n", courses[2].cCode, courses[2].cName, courses[2].cDescription, courses[2].studentRegistered, courses[2].maxRegister);

  for (i = 0; i < courses[2].studentRegistered; i++) {
    printf("%d - %s, %s \n", courses[2].registered[i]->num.studentNum, courses[2].registered[i]->lastName, courses[2].registered[i]->firstName);
  }
}

courses.h

#ifndef COURSES_H_
#define COURSES_H_

void createStudents();
void createCourses ();
void regiserStudents ();
void printCourses ();

#endif

structs.h

#ifndef STRUCTS_H_
#define STRUCTS_H_

char *firstName[] = {
  "Emma", "Liam", "Olivia",
  "Noah", "Ava", "Logan",
  "Sophia", "Lucas", "Isabella",
  "Mason", "Shaylyn", "Jack"
};

char *lastName[] = {
  "Smith", "Johnson", "Williams",
  "Brown", "Jones", "Miller",
  "Davis", "Garcia", "Rodriguez",
  "Wilson", "Seguin", "Loveday"
};

typedef struct{
  int studentNum;
}studentNumber;

typedef struct{
  char *firstName;
  char *lastName;
  studentNumber num;
}studentID;

studentID students[12];

char *courseName[] = {"Web Programming", "Technical Communication", "Processor Architecture"};
char *courseDescription[] = {"Learn to make websites!", "Learn the essentials of communication skills", "Learn the basics of circuits and Machine Language coding"};

typedef struct {
  int maxRegister;
  char *cCode;
  char *cName;
  char *cDescription;
  studentID *registered[8];
  studentID *waitlisted[12];
  int studentRegistered;
  int studentWaitlisted;
}course;

course courses[3];


#endif

本質上該程序應該運行:創建學生,創建隨機學校課程,填寫學生進入課程,然后打印所有內容,同時從頭文件訪問結構(structs.h)

這是我編譯后運行程序時收到的消息:

Segmentation fault (core dumped)

createStudents() ,您不需要取消引用firstName[i]lastName[i]

students[i].firstName = *firstName[i];
students[i].lastName = *lastName[i];

由於firstName[i]已經為您提供了char * ,因此您可以將該指針直接分配給students[i].firstName (同樣適用於lastName ):

students[i].firstName = firstName[i];
students[i].lastName = lastName[i];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM