简体   繁体   English

动态更改C中的结构数组

[英]Dynamically changing array of structures in C

I've been struggling to make a dynamically changing array of structures in C. My array size should change depending on how many times user decides to write data (city1 city2 and distance). 我一直在努力用C语言制作一个动态变化的结构数组。我的数组大小应该根据用户决定写入数据的次数(city1 city2和distance)而改变。 I'm talking about addEdge function, which is supposed to make a bigger array every time user inputs data; 我说的是addEdge函数,该函数应该在用户每次输入数据时创建一个更大的数组; it's also supposed to store structures in this array. 它也应该在该数组中存储结构。 I've used realloc function but it doesn't seem to work. 我使用过realloc函数,但它似乎不起作用。 In my main function I ask user to write his data twice and start my function after every input. 在我的主要功能中,我要求用户两次写入数据,并在每次输入后启动我的功能。 It, for some reason, works after first function initialization, but it crashes after the second time. 由于某种原因,它在第一个函数初始化之后可以工作,但是在第二次之后崩溃。 My intention is that it worked in a loop (user adds as much data as he wants). 我的意图是它可以循环工作(用户可以添加所需数量的数据)。 Here's the code: 这是代码:

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

typedef struct edge{
   char city1[30];
   char city2[30];
   int distance;
}edge;
void addEdge(edge **tab, char* city1, char* city2, int distance, int* n);
int main()
{
    edge *tab;
    tab=(edge*)malloc(0);
    char city1[30], city2[30];
    int distance, n=1;
        printf("\nType cities and distance in form: 'city1 city2 distance'\n");
    scanf("%s %s %d", city1, city2, &distance);
    addEdge(&tab, city1, city2, distance, &n);


        printf("\nType cities and distance in form: 'city1 city2 distance'\n");
    scanf("%s %s %d", city1, city2, &distance);
    addEdge(&tab, city1, city2, distance, &n);


    system("pause");
    return 0;
}


void addEdge(edge **tab, char* city1, char* city2, int distance, int* n)
{

    edge edgeN;
    strcpy(edgeN.city1, city1);
    strcpy(edgeN.city2, city2);
    edgeN.distance=distance;
    *tab=(edge*)realloc(*tab, *n * sizeof(edge));
    *tab[*n-1]=edgeN;
    *n=*n+1;

}

One of your mistake is that you never check any possible failure of your function call. 您的错误之一是您从不检查函数调用的任何可能失败。 scanf() return the number of field valid or an error, malloc() or realloc() can return an error. scanf()返回有效的字段数或错误, malloc()realloc()可以返回错误。

edge *tab = NULL;
size_t n = 1; // should be size_t and not int
// ...
if (scanf("%29s %29s %d", city1, city2, &distance) != 3) {
    fprintf(stderr, "wrong input\n");
    exit(EXIT_FAILURE);
}
// ...
edge *tmp = realloc(*tab, *n * sizeof *edge);
if (!tmp) {
  exit(EXIT_FAILURE);
}
*tab = tmp;

You have fallen in an operator precedence trap. 您陷入了运营商优先陷阱。 *tab[*n-1]=edgeN; is compiled as *(tab[*n - 1]) , when what you want is (*tab)[*n - 1]; 当您需要的是(*tab)[*n - 1];编译为*(tab[*n - 1]) (*tab)[*n - 1]; .

When in doubt, use parens, they may be useless, but it would be harmless. 如有疑问,请使用括号,它们可能无用,但无害。

This is the real cause for your problems, but you should also follow Stargateur's advice and test the return values of all input and allocation functions. 这是造成问题的真正原因,但是您还应该遵循Stargateur的建议并测试所有输入和分配函数的返回值。

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

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