Home > database >  I can't access the fields on a struct array
I can't access the fields on a struct array

Time:05-23

I'm learning how to program and I reached a roadblock on something I've been making. Once I to the function I print the value of the disp field but it says "0" and then it gets stuck on the Input part.

typedef struct{
    char disp;
    int cod;
    float costH;
    float costS;
    char nom_emp[50];
    int cod_emp;
    float precio;
}REG;
int main(){
    int end=1;
    int cont, selector;
    REG *registro[28];
    for (int i = 0; i <= cont; i  ){
         IngresoRep(&registro[i], i);
         }
}

void IngresoRep(REG * data, int i1){
    //Input and control of device
    scanf("%c", &data->disp); getchar();
    while (data->disp != 'E' && data->disp != 'N' && data->disp != 'T'){
        printf("\nTry again ");
        scanf("%c", &data->disp); getchar();
    }

I suspect it may have something to do with the -> use but I'm not too knowledgeable in those departments. Also I don't want to use any "fancier" functions.

CodePudding user response:

You have multiple problems, all related to the registro array and how you use it:

REG *registro[28];

Here you have defined an array of 28 pointers to REG structures. But you leave all the pointers uninitialized (which means they will have indeterminate (read: garbage) values and are invalid to dereference).

Fortunately, you don'y actually dereference the pointers, instead you use the pointer-to operator on the pointers to get pointers to them:

IngresoRep(&registro[i], i);

The type of registro[i] is REG *. The type of &registro[i] is REG **. That's not what the IngresoRep function expects. Since you pass the wrong pointer, you will have undefined behavior.

To solve both problems, define registro as an array of REG structures, not pointers:

REG registro[28];  // 28 REG structure objects

Now using &registro[i] in the call to IngresoRep will be correct and match what the function expects.


Also, you should really make a forward declaration of the IngresoRep function before the main function:

// Forward declaration of the function
void IngresoRep(REG * data, int i1);

int main(){
    // ...
}

void IngresoRep(REG * data, int i1){
    // ...
}

With this forward declaration the compiler will be able to detect if you pass the wrong arguments to the function, and will warn you about it.

CodePudding user response:

REG registro[28]; // I removed *

registro is attempting to be an array of data type struct REF, so * symbol is not necessary:

  • Without * what you have is 28 elements of data type struct REG
  • With * what you have is 28 pointers to data type struct REG

Later the right way to address registro elements doesn't include the & operation, the name registro is a pointer to the first element, so registro i is a pointer to the i element

IngresoRep(registro i, i); // IngresoRep(&registro[i], i);

CodePudding user response:

I'm not too keen on C. But you don't have any data in your REG struct when you initiate your array (REG *registro[28]) therefore your data is probably is going to be set to default 0 for int, and null for character.

  • Related