在 .h 中声明的结构的填充数组,在 .c 中的函数内部
Filling array of a struct, that was declared in .h, inside function in .c
好的,所以我在我的 .h 文件中声明了这个结构:
typedef struct Vet32 {
char key;
int thrash[7];
} Vet32;
我的另一个 .c 文件中有一个函数,那不是我的 main.c,我想在其中一行中填充我的数组抖动,例如:
vector[i].thrash[] = { 1, 3, 9, 123, 85, 12, 875 };
但是当我尝试像这样填充我的数组时,我的 gcc 编译器总是出错。我使用的函数是:
void createVet(FILE* arq, Vet32 vector[22]) {
for (int i = 0; i < 22; i++ ) {
fscanf(arq, "%c", vector[i].key);
vector[i].thrash[7] = { 1, 3, 9, 123, 85, 12, 875 };
}
}
数组的长度始终如此。任何人都可以帮助我吗? C xD 中的这个小错误真的让我很烦。
您只能在声明数组时对其进行初始化。如果你想稍后填写它们,你需要使用循环或类似 memcpy
:
void createVet(FILE* arq, Vet32 vector[22]) {
for (int i = 0; i < 22; i++ ) {
fscanf(arq, "%c", &(vector[i].key));
int values[7] = { 1, 3, 9, 123, 85, 12, 875 };
for (int j = 0; j < 7; j++ ) {
vector[i].thrash[j] = values[j];
}
}
}
使用 memcpy
,而不是我之前添加的新 for
循环,您可以这样做:
int values[7] = { 1, 3, 9, 123, 85, 12, 875 };
memcpy(vector[i].thrash, values, sizeof(values));
你这里的是初始化的语法。初始化为变量赋予初始值,并且该值在编译时必须保持不变。这是一个重要的要求,因为它允许编译器直接发出值,例如在生成的二进制文件中的 数据段 中。
但是您尝试在作业中使用它。赋值会在运行时更改变量的值。
对于 c 的所有版本,不可能 分配给数组。所以最直接的方法来做你想实现的是分配每个单独的成员:
vector[i].thrash[0] = 1;
vector[i].thrash[1] = 3;
[...]
但是既然你用的是struct
,还有另一种方法。 struct
可以作为一个整体赋值,这可以从 复合文字 开始,IIRC 从 c99 开始。下面是一些说明这一点的示例代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct Vet32 {
char key;
int thrash[7];
} Vet32;
void initVet(Vet32 *self)
{
*self = (Vet32) {
.key = 0,
.thrash = { 1, 3, 9, 123, 85, 12, 875 }
};
}
int main(void)
{
Vet32 vet;
initVet(&vet);
for (int i = 0; i < 7; ++i)
{
printf("vet[%d] = %d\n", i, vet.thrash[i]);
}
return 0;
}
复合文字看起来像一个初始化器,但在括号中带有明确的类型符号,就像它是一个演员.
好的,所以我在我的 .h 文件中声明了这个结构:
typedef struct Vet32 {
char key;
int thrash[7];
} Vet32;
我的另一个 .c 文件中有一个函数,那不是我的 main.c,我想在其中一行中填充我的数组抖动,例如:
vector[i].thrash[] = { 1, 3, 9, 123, 85, 12, 875 };
但是当我尝试像这样填充我的数组时,我的 gcc 编译器总是出错。我使用的函数是:
void createVet(FILE* arq, Vet32 vector[22]) {
for (int i = 0; i < 22; i++ ) {
fscanf(arq, "%c", vector[i].key);
vector[i].thrash[7] = { 1, 3, 9, 123, 85, 12, 875 };
}
}
数组的长度始终如此。任何人都可以帮助我吗? C xD 中的这个小错误真的让我很烦。
您只能在声明数组时对其进行初始化。如果你想稍后填写它们,你需要使用循环或类似 memcpy
:
void createVet(FILE* arq, Vet32 vector[22]) {
for (int i = 0; i < 22; i++ ) {
fscanf(arq, "%c", &(vector[i].key));
int values[7] = { 1, 3, 9, 123, 85, 12, 875 };
for (int j = 0; j < 7; j++ ) {
vector[i].thrash[j] = values[j];
}
}
}
使用 memcpy
,而不是我之前添加的新 for
循环,您可以这样做:
int values[7] = { 1, 3, 9, 123, 85, 12, 875 };
memcpy(vector[i].thrash, values, sizeof(values));
你这里的是初始化的语法。初始化为变量赋予初始值,并且该值在编译时必须保持不变。这是一个重要的要求,因为它允许编译器直接发出值,例如在生成的二进制文件中的 数据段 中。
但是您尝试在作业中使用它。赋值会在运行时更改变量的值。
对于 c 的所有版本,不可能 分配给数组。所以最直接的方法来做你想实现的是分配每个单独的成员:
vector[i].thrash[0] = 1;
vector[i].thrash[1] = 3;
[...]
但是既然你用的是struct
,还有另一种方法。 struct
可以作为一个整体赋值,这可以从 复合文字 开始,IIRC 从 c99 开始。下面是一些说明这一点的示例代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct Vet32 {
char key;
int thrash[7];
} Vet32;
void initVet(Vet32 *self)
{
*self = (Vet32) {
.key = 0,
.thrash = { 1, 3, 9, 123, 85, 12, 875 }
};
}
int main(void)
{
Vet32 vet;
initVet(&vet);
for (int i = 0; i < 7; ++i)
{
printf("vet[%d] = %d\n", i, vet.thrash[i]);
}
return 0;
}
复合文字看起来像一个初始化器,但在括号中带有明确的类型符号,就像它是一个演员.