简体   繁体   English

在 function 中初始化的结构丢失值(虽然它是一个数组)

[英]Struct initialized in function loses values (although it's an array)

I'm trying to initialize array *dip inside "leggif1", inside it if you do a print it's all normal but if you try to print in the main, after the initialization, everything loses its values.我正在尝试在“leggif1”中初始化数组 *dip,如果您在其中进行打印,则一切正常,但是如果您尝试在主目录中打印,则在初始化后,所有内容都会丢失其值。 Same thing happen with ADT of first grade "Divisione" and i can't understand why (even though they are passed "by reference" thanks to their pointers).一年级“Divisione”的 ADT 也会发生同样的事情,我不明白为什么(尽管由于他们的指针,它们是“通过引用”传递的)。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
  int matricola;
  char nome[20+1],cognome[20+1];
  int comp[4];            
}dipendente;

typedef struct divisione  *Divisione;
struct divisione{
dipendente *dip;
char nome[10+1];
int terna[4][3];        //numero minimo di addetti,competenza minima totale, competenza ottimale totale
                        //per ognuna delle 4 tipologie
};

void leggif1(dipendente *dip, char *filename);
int leggif2(Divisione *Div, char *filename);
void DIVstampa(Divisione *Div,char *filename,int D);
Divisione DIVinit();
void DIVfree(Divisione *Div);

int main(int argc,char **argv) {
dipendente *dip;
Divisione *Div;
leggif1(dip,argv[1]);
int D=leggif2(Div, argv[2]);
DIVstampa(Div,"stdout",D);

return 0;
}

void leggif1(dipendente *dip, char *filename) {
FILE *fp=fopen(filename,"r");
int i,N;

fscanf(fp,"%d",&N);
dip=malloc(N*sizeof(dipendente));

for(i=0;i<N;i++)
    fscanf(fp,"%d %s %s %d %d %d %d",&dip[i].matricola,dip[i].nome,dip[i].cognome,
           &dip[i].comp[0],&dip[i].comp[1],&dip[i].comp[2],&dip[i].comp[3]);

}

int leggif2(Divisione *Div, char *filename) {
FILE *fp=fopen(filename,"r");
int i,j,D;
fscanf(fp,"%d",&D);
Div=malloc(D*sizeof(Divisione));
for(i=0;i<D;i++)
    Div[i]=DIVinit();

for(i=0;i<D;i++) {
    fscanf(fp, "%s", Div[i]->nome);
    for (j = 0; j < 4; j++)
        fscanf(fp, "%d %d %d", &Div[i]->terna[j][0], &Div[i]->terna[j][1], &Div[i]->terna[j][2]);
}

return D;
}

void DIVstampa(Divisione *Div, char *filename, int D) {
FILE *fp;
if(strcmp(filename,"stdout")==0)
    fp=stdout;
else
    fp=fopen(filename,"w");

int i,j;
for(i=0;i<D;i++) {
    fprintf(fp,"%s\n", Div[i]->nome);
    for(j=0;j<4;j++)
        fprintf(fp,"%d %d %d\n", Div[i]->terna[j][0], Div[i]->terna[j][1], Div[i]->terna[j][2]);
}

}
Divisione DIVinit(){
Divisione Div=malloc(sizeof (*Div));
return Div;
}

void DIVfree(Divisione *Div){
free(Div);
}

The leggif1 function ignores the value of dip and assigns it a new value. leggif1 function 忽略dip的值并为其分配一个新值。 That value is never returned to main.该值永远不会返回给 main。

The type of dip is dipendente* and when called in main the value of the pointer is passed to the function. dip的类型是dipendente* ,当在 main 中调用时,指针的值被传递给 function。 Overwriting that local copy in the function does not affect the value of the pointer in main .覆盖 function 中的本地副本不会影响main中指针的值。

C only has 'call by value', always make sure you known what that value represents. C 只有“按值调用”,始终确保您知道该值代表什么。

This can be solved by returning the dip from the function instead of taking it as a parameter:这可以通过从 function返回dip而不是将其作为参数来解决:

dipendente* leggif1(char *filename)
{
    //open file and read N
    dipendente *dip = malloc(N * sizeof *dip);
    if (!dip) {
        return NULL;
    }
    // read in the data

    return dip;
}

another way is to use a dipendente** (a pointer to a pointer) but that would, in this case, make the code needlessly complex.另一种方法是使用dipendente** (指向指针的指针),但在这种情况下,这会使代码变得不必要地复杂。

The leggif2 function has the same problem. leggif2 function 也有同样的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM