通过指针访问结构体数组
这里我有一个结构定义:
typedef struct person {
unsigned int ssn, age, height, weight, income;
char name[MAXS+1], job[MAXS+1], religion[MAXS+1], major[MAXS+1], minor[MAXS+1], gender;
}PERSON;
这里我有一个 PERSON 类型定义的函数内结构数组:
PERSON record[MAXR+1];
我如何将此数组传递给另一个函数以进行实时更新(即作为指针)?
我的直觉和先验知识告诉我要执行以下操作:
PERSON *rp[MAXR+1];
for(i=0; i<MAXR; i++){
*rp[i]=&record[i];
}
valid=readfile(fp, rp);
但是,我收到“赋值中的类型不兼容”错误。 这样做的正确方法是什么?
这是完整的代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAXS 19
#define MAXR 999
typedef struct person {
unsigned int ssn, age, height, weight, income;
char name[MAXS+1], job[MAXS+1], religion[MAXS+1], major[MAXS+1], minor[MAXS+1], gender;
}PERSON;
//get and check ssn
int getssn(){
int num;
printf("\nSSN: ");
scanf("%d", &num);
if(num<=99999999 || num>999999999){
printf("\nPlease input a valid SSN.\n");
return 0;
}
else
return num;
}
int readfile(FILE *fptr, PERSON **rptr){
int v=0, i, j;
for(i=0; i<MAXR; i++){
j=i;
if(fscanf(fptr, "%c\n%d\n%19s\n%d\n%19s\n%d\n%19s\n%19s\n%d\n%d\n%19s\n\n",
&rptr[j]->gender, &rptr[j]->ssn, rptr[j]->name, &rptr[j]->age,
rptr[j]->job, &rptr[j]->income, rptr[j]->major, rptr[j]->minor,
&rptr[j]->height, &rptr[j]->weight, rptr[j]->religion)==EOF)
i=MAXR;
if(&rptr[MAXR]->ssn==&rptr[j]->ssn)
v=j;
}
return v;
}
int main(){
int valid=0, i;
char filename[MAXS]="clients.txt";
FILE *fp;
PERSON record[MAXR+1], *rp[MAXR+1];
do{
record[MAXR].ssn=getssn();
}while(record[MAXR].ssn==0);
printf("Name of file of records: ");
//gets(filename);
if((fp=fopen(filename, "r"))==NULL)
printf("\nCould not open file\n");
else{
printf("\njur doing gohd\n");
for(i=0; i<MAXR; i++){
*rp[i]=&record[i];
}
valid=readfile(fp, rp);
if(valid==0){
printf("\nSSN %d is not found in file %s.\n", record[MAXR].ssn, filename);
}
else {
printf("%d", valid);
}
}
return 0;
}
Here I have a structure definition:
typedef struct person {
unsigned int ssn, age, height, weight, income;
char name[MAXS+1], job[MAXS+1], religion[MAXS+1], major[MAXS+1], minor[MAXS+1], gender;
}PERSON;
And here I have an in-function array of structures of type PERSON definition:
PERSON record[MAXR+1];
How would I pass this array to another function for live updating (i.e. as a pointer)?
My intuition and prior knowledge tells me to do the following:
PERSON *rp[MAXR+1];
for(i=0; i<MAXR; i++){
*rp[i]=&record[i];
}
valid=readfile(fp, rp);
However, I get an "incompatible types in assignment" error.
What's the proper way of doing this?
Here is the full code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAXS 19
#define MAXR 999
typedef struct person {
unsigned int ssn, age, height, weight, income;
char name[MAXS+1], job[MAXS+1], religion[MAXS+1], major[MAXS+1], minor[MAXS+1], gender;
}PERSON;
//get and check ssn
int getssn(){
int num;
printf("\nSSN: ");
scanf("%d", &num);
if(num<=99999999 || num>999999999){
printf("\nPlease input a valid SSN.\n");
return 0;
}
else
return num;
}
int readfile(FILE *fptr, PERSON **rptr){
int v=0, i, j;
for(i=0; i<MAXR; i++){
j=i;
if(fscanf(fptr, "%c\n%d\n%19s\n%d\n%19s\n%d\n%19s\n%19s\n%d\n%d\n%19s\n\n",
&rptr[j]->gender, &rptr[j]->ssn, rptr[j]->name, &rptr[j]->age,
rptr[j]->job, &rptr[j]->income, rptr[j]->major, rptr[j]->minor,
&rptr[j]->height, &rptr[j]->weight, rptr[j]->religion)==EOF)
i=MAXR;
if(&rptr[MAXR]->ssn==&rptr[j]->ssn)
v=j;
}
return v;
}
int main(){
int valid=0, i;
char filename[MAXS]="clients.txt";
FILE *fp;
PERSON record[MAXR+1], *rp[MAXR+1];
do{
record[MAXR].ssn=getssn();
}while(record[MAXR].ssn==0);
printf("Name of file of records: ");
//gets(filename);
if((fp=fopen(filename, "r"))==NULL)
printf("\nCould not open file\n");
else{
printf("\njur doing gohd\n");
for(i=0; i<MAXR; i++){
*rp[i]=&record[i];
}
valid=readfile(fp, rp);
if(valid==0){
printf("\nSSN %d is not found in file %s.\n", record[MAXR].ssn, filename);
}
else {
printf("%d", valid);
}
}
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您试图将 PERSON 变量的地址分配给实际的 PERSON 变量(而不是地址),您可能打算这样做:
请注意,它将复制指向结构的指针,而不是结构本身。如果要复制结构,需要使用
memcpy
或memmove
You are trying to assign an address of PERSON variable to an actual PERSON variable (not address), you probably meant to do:
Note that it will copy the pointer to the struct, not the struct itself. If you want to copy structure, you need to use
memcpy
ormemmove
你只需要:
一个数组 - 定义明智 - 只是指向你定义的结构的一级指针。
不需要指针到指针,因为您在函数外部声明数组。如果您使用 malloc 在函数内分配内存,那么您需要一个指向指针的指针来返回分配内存的指针 - 但不是您的情况。
you just need:
An array - definition wise - is just one-level pointer to the structure you have defined.
there's no need of pointer-to-pointer because you are declaring array outside of the function. If you do
malloc
to allocate memory inside the function then you need a pointer-to-pointer to return the pointer the memory was allocated - not your case though.还值得指出 fptr[i]->job 中隐含的括号。
它是 (*fptr)[i],而不是 *fptr[i] 或 *(fptr[i])。也就是说,你想要索引
在您跟随指针之后,而不是之前。
It's also worth pointing out the parenthesis implicit in fptr[i]->job.
It's (*fptr)[i], not *fptr[i] or *(fptr[i]). That is, you want to index
after you follow the pointer, not before.
数组不能直接传递给函数 - 如果您尝试,它只会传递指向数组第一个元素的指针(可以像数组本身一样进行索引以访问数组的任何成员)。这就是为什么您可能会听到数组是“通过引用传递”的原因 - 尽管技术上并非如此,但这就是效果。
因此,您需要做的就是像这样声明您的函数:
...并且您可以像这样直接调用它:
根本不需要
rp
。Arrays cannot be passed directly to functions - if you try, it just passes a pointer to the first element of the array (which can be indexed just like the array itself to access any member of the array). This is why you might hear that arrays are "passed by reference" - although not technically true, that is the effect.
So, all you need to do is declare your function like this:
...and you can directly call it like this:
There's no need for
rp
at all.