C - 'struct'元素的动态数组

时间:2022-04-11 05:42:24

Based in this post I tried to do a dynamic array but, istead of a int array, an array of an struct designed in the program.

基于这篇文章,我尝试做一个动态数组,但不是一个int数组,一个在程序中设计的结构数组。

I can't make it work and I'd like to know where are my mistakes (mostly beacuse of working with pointers)

我不能让它工作,我想知道我的错误在哪里(主要是因为使用指针)

Any help would be appreciated.

任何帮助,将不胜感激。

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


//////////////////////////////////////

typedef struct {

  int group[8];
  uint64_t points;

} BestGroup;

//////////////////////////////////////

typedef struct {
  BestGroup *array;
  size_t used;
  size_t size;
} Array;

void initArray(Array *a, size_t initialSize) {
  a->array = (BestGroup *)malloc(initialSize * sizeof(BestGroup));
  a->used = 0;
  a->size = initialSize;
}

void insertArray(Array *a, int group_add, uint64_t points_add) {
  // a->used is the number of used entries, because a->array[a->used++] updates a->used only *after* the array has been accessed.
  // Therefore a->used can go up to a->size 
  if (a->used == a->size) {
    a->size *= 2;
    a->array = (BestGroup *)realloc(a->array, a->size * sizeof(BestGroup));
  }

  int i;

  for (i = 0; i < 8; i++)
  {
    a->array[a->used][i].group[i] = group_add;
  }
  a->array[a->used].points = points_add;
  a->used++;
}

void freeArray(Array *a) {
  free(a->array);
  a->array = NULL;
  a->used = a->size = 0;
}

///////////////////////////////////////////////////////////////////////////////

int main()

{
    Array a;
    int i;
    int list[8] = {0, 1, 2, 2, 4, 5, 6, 7};

    initArray(&a, 5);  // initially 5 elements
    for (i = 0; i < 100; i++)
      insertArray(&a, list, i);  // automatically resizes as necessary
    printf("%d\n", a.array.group[1]);  // print 2nd element
    printf("%lu\n", a.used);  // print number of elements
    freeArray(&a);
}

1 个解决方案

#1


2  

There are 3 typos:

有3个拼写错误:

void insertArray(Array *a, int group_add, uint64_t points_add)

should be

void insertArray(Array *a, int *group_add, uint64_t points_add)

You want to add an array of elements, so you have to give the function a pointer to the elements.

您想要添加元素数组,因此您必须为函数指定元素。

you edited this part while I wrote this answer to compensate the compile time error because of the first typo:

你编写这个部分时我编辑了这个部分来补偿编译时错误,因为第一个拼写错误:

a->array[a->used][i].group[i] = group_add[i];

or (your second version):

或(您的第二个版本):

a->array[a->used][i].group[i] = group_add;

should be

a->array[a->used].group[i] = group_add[i];

a->array[a->used] is already an object of the struct, no need to dereference it once again.

a-> array [a-> used]已经是struct的一个对象,不需要再次取消引用它。

and last but not least

最后但并非最不重要

printf("%d\n", a.array.group[1]);

should be

printf("%d\n", a.array->group[1]);

a.array is an array, so to get the first element, you can do a.array->group... or a.array[0].group...

a.array是一个数组,所以要获得第一个元素,你可以做a.array-> group ...或a.array [0] .group ...

a complete version: https://ideone.com/0WiXwO

完整版:https://ideone.com/0WiXwO

#1


2  

There are 3 typos:

有3个拼写错误:

void insertArray(Array *a, int group_add, uint64_t points_add)

should be

void insertArray(Array *a, int *group_add, uint64_t points_add)

You want to add an array of elements, so you have to give the function a pointer to the elements.

您想要添加元素数组,因此您必须为函数指定元素。

you edited this part while I wrote this answer to compensate the compile time error because of the first typo:

你编写这个部分时我编辑了这个部分来补偿编译时错误,因为第一个拼写错误:

a->array[a->used][i].group[i] = group_add[i];

or (your second version):

或(您的第二个版本):

a->array[a->used][i].group[i] = group_add;

should be

a->array[a->used].group[i] = group_add[i];

a->array[a->used] is already an object of the struct, no need to dereference it once again.

a-> array [a-> used]已经是struct的一个对象,不需要再次取消引用它。

and last but not least

最后但并非最不重要

printf("%d\n", a.array.group[1]);

should be

printf("%d\n", a.array->group[1]);

a.array is an array, so to get the first element, you can do a.array->group... or a.array[0].group...

a.array是一个数组,所以要获得第一个元素,你可以做a.array-> group ...或a.array [0] .group ...

a complete version: https://ideone.com/0WiXwO

完整版:https://ideone.com/0WiXwO