具有这种类型的结构,如何将一个结构复制到另一种结构中。我声明了 Array first
和 Array second
,然后我启动了两者,并在每个数据中放置了不同的数据。
然后将first
复制到second
,我尝试了second = first
,但它不起作用。
我该怎么做?
#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].group[i] = group_add[i];
}
a->array[a->used].points = points_add;
a->used++;
}
void freeArray(Array *a) {
free(a->array);
a->array = NULL;
a->used = a->size = 0;
}
void CopyArray(Array *a, Array *b)
{
b = a;
}
int main()
{
Array first;
Array second;
int first_data[8] = {0, 1, 2, 3, 4, 5, 6, 7};
int second_data[8] = {7, 6, 5, 4, 3, 2, 1, 0};
initArray(&first, 2);
initArray(&second, 2);
insertArray(&first, first_data, 5);
insertArray(&first, first_data, 5);
insertArray(&first, first_data, 5);
insertArray(&second, second_data, 2);
///////////////////////////////////////////
printf("Total Points: %" PRIu64 "n", first.array->points);
printf("Number: %lunn", first.used);
printf("n");
int i;
int j;
for (i = 0; i < first.used; i++)
{
printf("[");
for (j = 0; j < 8; j++)
{
if (j) printf(", ");
printf("%d", first.array[i].group[j]);
}
printf("]n");
}
////////////////////////////////////////////
printf("n");
printf("Total Points: %" PRIu64 "n", second.array->points);
printf("Number: %lunn", second.used);
printf("n");
for (i = 0; i < second.used; i++)
{
printf("[");
for (j = 0; j < 8; j++)
{
if (j) printf(", ");
printf("%d", second.array[i].group[j]);
}
printf("]n");
}
/////////////////////////////////
CopyArray(&first, &second);
printf("n");
printf("n");
printf("n");
printf("n");
///////////////////////////////////////////
printf("Total Points: %" PRIu64 "n", first.array->points);
printf("Number: %lunn", first.used);
printf("n");
for (i = 0; i < first.used; i++)
{
printf("[");
for (j = 0; j < 8; j++)
{
if (j) printf(", ");
printf("%d", first.array[i].group[j]);
}
printf("]n");
}
////////////////////////////////////////////
printf("n");
printf("Total Points: %" PRIu64 "n", second.array->points);
printf("Number: %lunn", second.used);
printf("n");
for (i = 0; i < second.used; i++)
{
printf("[");
for (j = 0; j < 8; j++)
{
if (j) printf(", ");
printf("%d", second.array[i].group[j]);
}
printf("]n");
}
return 0;
}
输出为:
Total Points: 5
Number: 3
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
Total Points: 2
Number: 1
[7, 6, 5, 4, 3, 2, 1, 0]
Total Points: 5
Number: 3
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
Total Points: 2
Number: 1
[7, 6, 5, 4, 3, 2, 1, 0]
应以:
结尾的最后列表结尾Total Points: 5
Number: 3
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
Total Points: 5
Number: 3
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
编辑
建议我使用memcpy(),所以我将CopyArray()
函数更改为:
void CopyArray(Array *a, Array *b)
{
// b = a;
memcpy(b, a, a->size * sizeof(BestGroup));
}
看起来结果会没事的,直到达到程序的末尾并输出
*** stack smashing detected ***
此代码产生您想要的答案。CopyArray()
函数现在释放在b
中的数据,然后在b
初始化,并最终将数据从a
复制到b
。在某些地方,代码使用8
,但可能应该为大小使用不同的变量 - 我已经在此代码中对它们进行了注释。dump_array()
函数是我在调试复杂结构时常规编写和使用的函数的一个示例。它采用标签字符串(该标签字符串(它允许您识别哪个称为打印)以及要打印的对象。我也经常接受文件流参数,如果有疑问,请为输出流添加fflush()
。
/* SO 4639-4467 */
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct
{
int group[8];
uint64_t points;
} BestGroup;
typedef struct
{
BestGroup *array;
size_t used;
size_t size;
} Array;
static void initArray(Array *a, size_t initialSize)
{
a->array = (BestGroup *)malloc(initialSize * sizeof(BestGroup));
a->used = 0;
a->size = initialSize;
}
static void insertArray(Array *a, const int *group_add, uint64_t points_add)
{
if (a->used == a->size)
{
a->size *= 2;
a->array = (BestGroup *)realloc(a->array, a->size * sizeof(BestGroup));
}
for (int i = 0; i < 8; i++) // Why 8 and not points_add?
{
a->array[a->used].group[i] = group_add[i];
}
a->array[a->used].points = points_add;
a->used++;
}
static void freeArray(Array *a)
{
free(a->array);
a->array = NULL;
a->used = a->size = 0;
}
static void CopyArray(const Array *a, Array *b)
{
freeArray(b);
initArray(b, a->used);
memmove(b->array, a->array, a->used * sizeof(a->array[0]));
b->used = a->used;
}
static void dump_array(const char *tag, const Array *arr)
{
printf("Array: %sn", tag);
printf("Total Points: %" PRIu64 "n", arr->array->points);
printf("Number: %lun", arr->used);
for (size_t i = 0; i < arr->used; i++)
{
printf("[");
for (size_t j = 0; j < 8; j++) // Why 8 and not arr->array[i].points?
{
if (j)
printf(", ");
printf("%d", arr->array[i].group[j]);
}
printf("]n");
}
putchar('n');
}
int main(void)
{
Array first;
Array second;
int first_data[8] = {0, 1, 2, 3, 4, 5, 6, 7};
int second_data[8] = {7, 6, 5, 4, 3, 2, 1, 0};
initArray(&first, 2);
initArray(&second, 2);
insertArray(&first, first_data, 5);
insertArray(&first, first_data, 5);
insertArray(&first, first_data, 5);
insertArray(&second, second_data, 2);
dump_array("first", &first);
dump_array("second", &second);
CopyArray(&first, &second);
printf("n");
dump_array("first", &first);
dump_array("second", &second);
return 0;
}
运行的输出是:
Array: first
Total Points: 5
Number: 3
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
Array: second
Total Points: 2
Number: 1
[7, 6, 5, 4, 3, 2, 1, 0]
Array: first
Total Points: 5
Number: 3
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
Array: second
Total Points: 5
Number: 3
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]
[0, 1, 2, 3, 4, 5, 6, 7]