[英]Pointer to array of struct in function
I had a problem with the pointers.我的指针有问题。 I wanna read a binary file with a function, and then, use the read data in the main.
我想用一个函数读取一个二进制文件,然后在主文件中使用读取的数据。 The problem is that I had to pass a pointer to array of struct to can use the data in main.
问题是我必须传递一个指向结构数组的指针才能使用 main 中的数据。
The code is:代码是:
#define TMOLDEO 8
#define TAM 41
struct fichpiezas{
int codPieza;
float dimPieza;
float costePieza[TMOLDEO];
};
int leer_fichero(struct fichpiezas *vpiezas[]);
int main(int argc, const char * argv[]) {
struct fichpiezas vpiezas[TAM];
leer_fichero(&vpiezas);
for(int i = 0; sizeof(vpiezas)/sizeof(struct fichpiezas); i++){
printf("Codigo pieza : %d\n", vpiezas[i].codPieza);
}
return 0;
}
int leer_fichero (struct fichpiezas *vpiezas[]){
FILE *fich;
struct fichpiezas pieza;
int error_dev = 0, i = 0;
if ((fich = fopen("piezas.bin", "rb")) == NULL){
printf ( "Error en apertura del fichero para lectura \n " );
error_dev = 1;
} else{
//Bucle mientras no sea fin de fichero
while (! feof(fich)){
fread (&pieza, sizeof(pieza), 1, fich);
vpiezas[i] = &pieza;
i++;
}
fclose (fich);
}
return error_dev;
}
Just change this只需改变这个
int leer_fichero (struct fichpiezas *vpiezas[])
to到
int leer_fichero (struct fichpiezas *vpiezas)
and in your main()
并在您的
main()
leer_fichero(&vpiezas);
to到
leer_fichero(vpiezas);
the array will automatically decay to a pointer when passed to the function.传递给函数时,数组将自动衰减为指针。 So you don't need to pass it's address to the function.
所以你不需要将它的地址传递给函数。
You have another problem, this assignment你还有一个问题,这个作业
vpiezas[i] = &pieza;
is a problem, because you are storing the address of the local variable pieza
in the array, and the data will be gone when this function returns.是有问题的,因为你在数组中存储的是局部变量
pieza
的地址,这个函数返回的时候数据就没了。
Aditionally the value of pieza
is being overwritten in each iteration bu fread()
and since you store the address of pieza
instead of it's value, all the elements of the array will have the same value if it succeeded this way. pieza
,在每次迭代 bu fread()
, pieza
的值pieza
被覆盖,并且由于您存储的是pieza
的地址而不是它的值,因此如果以这种方式成功,则数组的所有元素都将具有相同的值。
You need to copy the struct into the array element, and this line if you follow my advice above should change to您需要将结构复制到数组元素中,如果您按照我上面的建议,这一行应该更改为
vpiezas[i] = pieza;
or the program will fail to compile, because the type of &pieza
is struct fichpiezas *
and that of vpiezas[i]
now is struct fichpiezas
after changing the function prototype.否则程序将无法编译,因为
&pieza
的类型是struct fichpiezas *
而vpiezas[i]
struct fichpiezas
在更改函数原型后现在是struct fichpiezas
。
Also, about while (! feof(fich))
there was an answer which explains in detail why that is wrong.此外,关于
while (! feof(fich))
有一个答案,详细解释了为什么这是错误的。
And one more thing, add a check i < TAM
because you are in risk of overflowing the array otherwise.还有一件事,添加一个检查
i < TAM
因为否则你有溢出数组的风险。
This这个
int leer_fichero(struct fichpiezas * vpiezas[]);
defines vpiezas
to be a pointer to a pointer to struct fichpiezas
, as将
vpiezas
定义为指向struct fichpiezas
的指针,如
int leer_fichero(struct fichpiezas * vpiezas[]);
is equivalent to相当于
int leer_fichero(struct fichpiezas ** vpiezas);
To address the elements of the array you pass by doing要通过执行来解决您传递的数组元素
leer_fichero(&vpiezas);
do like this这样做
(*vpiezas)[index]
inside of leer_fichero()
.在
leer_fichero()
内部。
It would be more straight forward to define定义会更直接
int leer_fichero(struct fichpiezas * vpiezas);
and pass并通过
leer_fichero(vpiezas);
then you can address the elements as you do:然后你可以像你一样处理这些元素:
vpiezas[index]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.