C中的动态结构数组 [英] dynamic array of structs in C

查看:90
本文介绍了C中的动态结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习C语言中的结构,指针和动态数组.我不了解如何使用指针创建结构的动态数组.我的代码不起作用,我也不知道这是怎么回事.我已经看到了一些动态数组的示例,但是没有关于结构的示例.任何帮助,将不胜感激.请给出一些解释,而不仅仅是代码片段,因为我确实想了解不仅解决此问题.

I am trying to learn about structs, pointers, and dynamic arrays in C. I don't understand how to create a dynamic array of structs using pointers. My code doesn't work, and I don't know what's wrong with it. I have seen several examples of dynamic arrays, but non with structs. Any help would be appreciated. Please give some explanation, not just code snippets as I do want to understand not just solve this problem.

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

struct *struct_array;
int i,m,n,p;

struct data
{
    char inputA[20];
    char inputB[20];    
};

struct data get_data()
{
    struct data thisdata;

    printf("Please enter input A\n");
    scanf("%s", thisdata.inputA);

    printf("Please enter input B\n");
    scanf("%s", thisdata.inputB);

    return thisdata;
}

void Output(struct data struct_array, int n)
{
    int index = 0;
    for(i = 0; i<n ;i++)
    {
        printf("%s ", struct_array[i].inputA);
        printf("%s ", struct_array[i].inputB);
    }   
}

void resizeArray(int n)
{
    struct_array = (int*)realloc(n*sizeof(int));
}

void mainMenu()
{
    printf("Please select from the following options:\n");
    printf("1: Add new students to database\n");
    printf("2: Display current student database contents\n");
    printf("3: exit the program\n");
    scanf("%d", &p);
    if(p == 1)
    {
        printf("Please enter the number of students to register:\n");
        scanf("%d", &n);
        resizeArray(n);
        for(i = n; i<n ;i++)
        {
            struct_array[i] = get_data();
        }
    }
    else if(p == 2)
    {
         Output(struct_array, n);
    }
    else
    {
        free(struct_array);
        exit(0);
    }        
}

int main()
{    
    struct_array = (int*)realloc(2*sizeof(int));
    mainMenu();
}

推荐答案

您的源代码中存在几个错误:

You have several errors in your source code:

  • struct *struct_array;(l.5)
    这是什么意思?您要写struct data *struct_array吗?

  • struct *struct_array; (l. 5)
    What does it mean? Did you want to write struct data *struct_array?

printf("%s ", struct_array[i].inputA);(l.32& l.33)
参数struct_array掩盖了全局声明,它不是数组.为什么要添加此参数?

printf("%s ", struct_array[i].inputA); (l.32 & l. 33)
The argument struct_array masks the global declaration, and it is not an array. Why did you add this argument?

struct_array = (int *)realloc(n * sizeof(int));(l.39)
你忘了一个争论.您是否要使用malloc?此外,强制转换不是必需的(而且不正确!).

struct_array = (int *)realloc(n * sizeof(int)); (l. 39)
You have forgotten an argument. Did you want to use malloc instead? Besides, the cast is not necessary (and incorrect!).

除非使用的是托管环境和C99/C11,否则应从main返回一个值.

Unless you are using an hosted environnment and C99/C11, you should return a value from main.

您的变量index未使用.为什么要声明它?

Your variable index is not used. Why did you declare it?

for(i = n; i < n; i++)(l.53) 您在这里不会有任何迭代...

for(i = n; i < n; i++) (l. 53) You won't have any iteration here...

以下代码按预期工作.

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

/* TODO: Avoid global variables. */
struct data *struct_array;

struct data {
    char inputA[20];
    char inputB[20];
};

/* 
 * TODO: Try to avoid passing your structure (40 bytes + padding) 
 * without pointer. 
 */
struct data get_data(void)
{
    struct data thisdata;

    printf("Please enter input A\n");

    /* TODO: Avoid using `scanf` for human inputs. */
    scanf("%s", thisdata.inputA);

    printf("Please enter input B\n");
    scanf("%s", thisdata.inputB);

    return thisdata;
}

void Output(size_t n)
{
    size_t i;
    for (i = 0; i < n; i++) {
        printf("%s ", struct_array[i].inputA);
        printf("%s ", struct_array[i].inputB);
    }
}

void resizeArray(size_t n)
{
    /* TODO: Handle reallocations errors. */
    struct_array = realloc(struct_array, n * sizeof *struct_array);
}

void mainMenu(void)
{
    size_t i, n;
    int p;

    /* TODO: Use a loop ? */
    printf("Please select from the following options:\n");
    printf("1: Add new students to database\n");
    printf("2: Display current student database contents\n");
    printf("3: exit the program\n");
    scanf("%d", &p);

    switch (p) {
    case 1:
        printf("Please enter the number of students to register:\n");
        scanf("%u", &n);
        resizeArray(n);

        for (i = 0; i < n; i++)
            struct_array[i] = get_data();
        break;
    case 2:
        Output(n);
        break;
    }
}

int main(void)
{
    struct_array = malloc(2 * sizeof(int));
    mainMenu();
    free(struct_array);
    return 0;
}

这篇关于C中的动态结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆