通过指针访问结构体数组

发布于 2024-11-09 04:29:57 字数 2266 浏览 0 评论 0原文

这里我有一个结构定义:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

风向决定发型 2024-11-16 04:29:57

您试图将 PERSON 变量的地址分配给实际的 PERSON 变量(而不是地址),您可能打算这样做:

rp[i]=&record[i];

请注意,它将复制指向结构的指针,而不是结构本身。如果要复制结构,需要使用memcpymemmove

You are trying to assign an address of PERSON variable to an actual PERSON variable (not address), you probably meant to do:

rp[i]=&record[i];

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 or memmove

坚持沉默 2024-11-16 04:29:57

你只需要:

int readfile(FILE *fptr, PERSON *rptr);

一个数组 - 定义明智 - 只是指向你定义的结构的一级指针。

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;
}

不需要指针到指针,因为您在函数外部声明数组。如果您使用 malloc 在函数内分配内存,那么您需要一个指向指针的指针来返回分配内存的指针 - 但不是您的情况。

you just need:

int readfile(FILE *fptr, PERSON *rptr);

An array - definition wise - is just one-level pointer to the structure you have defined.

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;
}

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.

小帐篷 2024-11-16 04:29:57

还值得指出 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.

安静被遗忘 2024-11-16 04:29:57

数组不能直接传递给函数 - 如果您尝试,它只会传递指向数组第一个元素的指针(可以像数组本身一样进行索引以访问数组的任何成员)。这就是为什么您可能会听到数组是“通过引用传递”的原因 - 尽管技术上并非如此,但这就是效果。

因此,您需要做的就是像这样声明您的函数:

int readfile(FILE *fptr, PERSON rptr[])
{
    int v = 0, i;

    for(i = 0; i < MAXR; 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[i]->gender, &rptr[i]->ssn, rptr[i]->name, &rptr[i]->age, 
          rptr[i]->job, &rptr[i]->income, rptr[i]->major, rptr[i]->minor,
          &rptr[i]->height, &rptr[i]->weight, rptr[i]->religion) < 11)
            break;

        if (rptr[MAXR]->ssn == rptr[i]->ssn)
            v = i + 1;
    }

    return v;
}

...并且您可以像这样直接调用它:

valid = readfile(fp, record);

根本不需要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:

int readfile(FILE *fptr, PERSON rptr[])
{
    int v = 0, i;

    for(i = 0; i < MAXR; 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[i]->gender, &rptr[i]->ssn, rptr[i]->name, &rptr[i]->age, 
          rptr[i]->job, &rptr[i]->income, rptr[i]->major, rptr[i]->minor,
          &rptr[i]->height, &rptr[i]->weight, rptr[i]->religion) < 11)
            break;

        if (rptr[MAXR]->ssn == rptr[i]->ssn)
            v = i + 1;
    }

    return v;
}

...and you can directly call it like this:

valid = readfile(fp, record);

There's no need for rp at all.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文