1

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

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

0

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.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.