打印结构数组

时间:2015-10-26 05:05:40

标签: c arrays struct

addInfo()将信息添加到结构数组中,函数printList()输出该数组的所有元素。但我的printList()只输出添加的最后一个元素。有什么不对?它是否错误地将元素添加到数组中?

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

//structure Location
typedef struct Location{
    char locName[35];
    char locDesc[85];
    float latitude;
    float longitude;
} LocationArray;

void addInfo(LocationArray **myArray, int*, int*);
void printList(LocationArray **myArray, int*);
void quitProgram();
void resizeArray(LocationArray **myArray, int*);

//Menu that receives the user input
//and performs corresponding operations based
//on the menu choice
void printMenu(LocationArray **myArray, int *count, int *max){
    printf("Hello! Please choose from the menu: \n");
    printf("Type 'A' or 'a' for adding additional location to the array\n");
    printf("Type 'P' or 'p' for printing the current list of locations\n");
    printf("Type 'Q' or 'q' to quit the program\n");

    char input = 0;
    scanf(" %c", &input);

    //Handles the invalid character input
    //exits if the character does not correspond
    //to the valid menu option
    while(input != 'A' && input != 'a'
        && input != 'P' && input != 'p'
        && input != 'Q' && input != 'q'){
            printf("Invalid character! Try entering a char again: \n");
            scanf(" %c", &input);
    }
    //Calls other functions
    //based on the character input
    switch(input){
    case 'A':
    case 'a':
        addInfo(myArray, count, max); //Calls function that adds more locations into the array
        break;
    case 'P':
    case 'p':
        printList(myArray, count); //Calls the function that prints the current list of locations
        break;
    case 'Q':
    case 'q':
        quitProgram(); //Calls the function that terminates the program
        break;
    }
}

//Adds info into the array of structures
void addInfo(LocationArray **myArray, int *count, int *numberOfLoc){

    if(*count == *numberOfLoc){ // Checks if the array is already full
        resizeArray(myArray, numberOfLoc); //Resizes the array if it's full

        printf("Please enter the name for the location:\n");
        scanf(" %[^\n]", &(*myArray)->locName); //Gets the user input for the Location Name
        printf("\nNow enter the description:\n");
        scanf(" %[^\n]", &(*myArray)->locDesc); //Gets the user input for the Location Description
        printf("\nNow enter the value for the latitude:\n");
        scanf("%f", &(*myArray)->latitude); //Gets the user input for the Latitude
        printf("\nNow enter the value for the longitude:\n");
        scanf("%f", &(*myArray)->longitude); //Gets the user input for the Latitude

        (*count)++; //Increment the count
    }
    else{ //Else, a used fills it out
        printf("Please enter the name for the location:\n");
        scanf(" %[^\n]", &(*myArray)->locName); //Gets the user input for the Location Name
        printf("\nNow enter the description:\n");
        scanf(" %[^\n]", &(*myArray)->locDesc); //Gets the user input for the Location Description
        printf("\nNow enter the value for the latitude:\n");
        scanf("%f", &(*myArray)->latitude); //Gets the user input for the Latitude
        printf("\nNow enter the value for the longitude:\n");
        scanf("%f", &(*myArray)->longitude); //Gets the user input for the Latitude

        (*count)++; //Increment the count
    }
}

//Resizes(doubles) the array size if the max limit is achieved
void resizeArray(LocationArray **myArray, int *numberOfLoc){
    *numberOfLoc = *numberOfLoc * 2; //Double the size

    LocationArray *temp;
    temp = (LocationArray*)realloc(*myArray, *numberOfLoc *sizeof(LocationArray)); //Reallocates more memory

    //Checks if the memory heap is exhausted
    if(temp == NULL){
        printf("The memory heap is exhausted!\n");
    }
    else{
        *myArray = temp; //Copy from the temp struct variable to myArray
    }
}

//Prints all of the elements of the array
void printList(LocationArray **myArray, int *count){
    if((*count) == 0){ //If the list is empty, then return
        printf("The list is empty!");
        return;
    }

    int i;
    for(i = 0; i < (*count); i++){
        printf("Location name: %s\n", (*myArray[i]).locName);
        printf("Description: %s\n", (*myArray[i]).locDesc);
        printf("Latitude: %f\n", (*myArray[i]).latitude);
        printf("Longitude: %f\n", (*myArray[i]).longitude);
    }
}

//Quits the program
void quitProgram(){
    printf("Bye!");
    exit(0);
}


int main()
{
    printf("How many locations would you like to be inside the array?\n");
    int numberOfLoc = 0; //variable for storing the size of the LocationArray
    scanf(" %d", &numberOfLoc); //gets the user input and stores in numerOfLoc

    LocationArray *myArray; //declares a LocationArray with the size of numberOfLoc
    myArray = (LocationArray*)malloc(numberOfLoc*sizeof(LocationArray));

    int count = 0;
    while(1){
        //Prints the menu
        printMenu(&myArray, &count, &numberOfLoc);
    }
    //Free the pointer
    free(myArray);
    return 0;
}

1 个答案:

答案 0 :(得分:1)

printList中,您有

for(i = 0; i < (*count); i++){
    printf("Location name: %s\n", (*myArray[i]).locName);
    printf("Description: %s\n", (*myArray[i]).locDesc);
    printf("Latitude: %f\n", (*myArray[i]).latitude);
    printf("Longitude: %f\n", (*myArray[i]).longitude);
}

*myArray[i]的使用不正确。这相当于*(myArray[i])。您需要使用:(*myArray)[i]

for(i = 0; i < (*count); i++){
    printf("Location name: %s\n", (*myArray)[i].locName);
    printf("Description: %s\n", (*myArray)[i].locDesc);
    printf("Latitude: %f\n", (*myArray)[i].latitude);
    printf("Longitude: %f\n", (*myArray)[i].longitude);
}

通过将参数更改为,可以使printList中的代码更简单 LocationArray *myArrayint count

void printList(LocationArray *myArray, int count){
    if(count == 0){ //If the list is empty, then return
        printf("The list is empty!");
        return;
    }

    int i;
    for(i = 0; i < count; i++){
        printf("Location name: %s\n", myArray[i].locName);
        printf("Description: %s\n", myArray[i].locDesc);
        printf("Latitude: %f\n", myArray[i].latitude);
        printf("Longitude: %f\n", myArray[i].longitude);
    }
}

并确保更改对该功能的调用。而不是

printList(myArray, count);

使用

printList(*myArray, *count);

进一步清理

scanf(" %[^\n]", &(*myArray)->locName);
scanf(" %[^\n]", &(*myArray)->locDesc);
两个帐户

错误。

  1. 您无需使用&
  2. 您需要使用count的{​​{1}}个元素。以上内容始终将数据存储在myArray的第一个元素中。
  3. 使用

    myArray

    将其他scanf(" %[^\n]", (*myArray)[*count].locName); scanf(" %[^\n]", (*myArray)[*count].locDesc); 行修改为:

    scanf