简体   繁体   English

malloc 结构内部的结构数组

[英]malloc array of struct inside of struct

I have project in school where in need to make a struct of AirportManager which holds the amount of airports and an array of Airport (another struct).我在学校有一个项目,需要制作一个包含机场数量和机场数组(另一个结构)的 AirportManager 结构。 I started writing the code but I having trouble with the malloc of the array of airports.我开始编写代码,但我遇到了机场阵列的 malloc 的问题。
I attahced to code I write so far, the problem I have is that the values dont saved in the airportArray in the AirportManager.到目前为止,我附加了我编写的代码,我遇到的问题是这些值没有保存在 AirportManager 的 airportArray 中。

//AirportManger Struct
typedef struct {
    Airport* airportArray;
    int airportAmount;
}AirportManager;

void initAirportManager(AirportManager* airportManager) 
{
    airportManager->airportAmount = 0;
    airportManager->airportArray = (AirportManager*)malloc(0);
}
void addAirport(AirportManager* airportManager)
{
    Airport airport;
    printf("Enter Airport Name: ");
    scanf("%s", airport.airportName);
    printf("Enter Airport Address: ");
    scanf("%s", airport.airportAddress);
    
    airportManager->airportAmount++;
    airportManager->airportArray = (Airport*)realloc(airportManager->airportArray, airportManager->airportAmount * sizeof(Airport));
    airportManager->airportArray = airport;

}

//Airport Struct
typedef struct {
    char airportName[MAX];
    char airportAddress[MAX];
}Airport;

//Main
AirportManager airportManager;
initAirportManager(airportManager);
addAirport(&airportManager);

The code has some issues.代码有一些问题。 We shouln't:我们不应该:

  • allocate zero bytes with malloc(0)使用malloc(0)分配零字节
  • assign twice to airportManager->airportArray分配两次给airportManager->airportArray
  • use scanf使用scanf

Here is the code modified.这是修改后的代码。 It uses malloc and realloc better, and fgets instead of scanf .它使用mallocrealloc更好,并且fgets而不是scanf

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

#define SIZ 512

typedef struct {
    char airportName[SIZ];
    char airportAddress[SIZ];
} Airport;

typedef struct {
    Airport* airportArray;
    int airportAmount;
} AirportManager;

// Call first on AirportManager
void initAirportManager(AirportManager* airportManager)
{
    airportManager->airportAmount = 0;
    airportManager->airportArray = NULL;
}

// Call last on AirportManager
void finalAirportManager(AirportManager* airportManager)
{
    airportManager->airportAmount = 0;
    if (airportManager->airportArray != NULL)
        free(airportManager->airportArray);
    airportManager->airportArray == NULL;
}

// Add an airport to the manager
void addAirportByNameAndAddress(AirportManager* airportManager, char *name, char *address)
{
    // Calculate the amount of memory needed
    size_t mem = (airportManager->airportAmount + 1) * sizeof(Airport);

    // Get the memory required
    Airport* newAirports = NULL;
    if (airportManager->airportArray == NULL)
        newAirports = (Airport*)malloc(mem);
    else
        newAirports = (Airport*)realloc(airportManager->airportArray, mem);
    if (newAirports == NULL)
    {
        // error: out of memory
        return;
    }

    // Copy the name and the address to new the new Airport
    Airport *current = newAirports + airportManager->airportAmount;
    memset(current->airportName, '\0', SIZ);
    strncpy(current->airportName, name, SIZ - 1);
    memset(current->airportAddress, '\0', SIZ);
    strncpy(current->airportAddress, address, SIZ - 1);

    // Update the manager
    airportManager->airportAmount++;
    airportManager->airportArray = newAirports;
}

void addAirport(AirportManager* airportManager)
{
    char name[SIZ] = { 0 };
    char address[SIZ] = { 0 };
    printf("Enter Airport Name: ");
    fgets(name, SIZ - 1, stdin);
    printf("Enter Airport Address: ");
    fgets(address, SIZ - 1, stdin);
    addAirportByNameAndAddress(airportManager, name, address);
}

void main() {
    AirportManager airportManager;
    initAirportManager(&airportManager);
    addAirport(&airportManager);
    finalAirportManager(&airportManager);
}

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

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