简体   繁体   中英

Why is this struct passed as argument of a function, change is values?

I have this struct and this function:

typedef struct pares{
int x,y;
} PAR;

arvpre criararvore(
   int i, 
   int j, 
   char matriz[i][j], 
   int x, 
   int y, 
   int *c, 
   PAR vis[], 
   int k
   )

As you can see, I pass PAR vis as an array of structs. From what I've been taught, this way, the value of vis is declared outside the function; and therefore should remain unaltered, right? For example, If it's all zero before calling the function, after the function they all should still be zero. However, the values change after calling the function.

Here is the full code:

typedef struct arv *arvpre;

typedef struct arv {
   char valor;
   arvpre N[8];
   int flag;
}Nodo;

typedef struct pares{
   int x,y;
} PAR;

arvpre criararvore(
     int i, 
     int j, 
     char matriz[i][j], 
     int x, 
     int y, 
     int *c, 
     PAR vis[], 
     int k
     )
{
   arvpre a=NULL;
   int z;

   while (*c){
      if (x>=j || x<0 || y<0 ||y>=i || vizitado (vis,k,x,y)) {
            return NULL;
         }
      else {
            a=(arvpre) malloc (sizeof(Nodo));
            a->valor=matriz[y][x];
            vis[k].x=x; 
            vis[k].y=y; 
            k++;
            (*c)--;
            z=*c;
            a->N[0] = criararvore (i,j,matriz,x,y-1,c,vis,k);
            *c=z;
            a->N[1] = criararvore (i,j,matriz,x-1,y-1,c,vis,k);
            *c=z;
            a->N[2] = criararvore (i,j,matriz,x-1,y,c,vis,k);
            *c=z;
            a->N[3] = criararvore (i,j,matriz,x-1,y+1,c,vis,k);
            *c=z;
            a->N[4] = criararvore (i,j,matriz,x,y+1,c,vis,k);
            *c=z;
            a->N[5] = criararvore (i,j,matriz,x+1,y+1,c,vis,k);
            *c=z;
            a->N[6] = criararvore (i,j,matriz,x+1,y,c,vis,k);
            *c=z;
            a->N[7] = criararvore (i,j,matriz,x+1,y-1,c,vis,k);
            *c=z;

            return a;
         }
   }
}

int main(){
   int i,j,c, dimy=3,dimy=4, flag=0;
   PAR vis[23];
   char sopal[3][4]={{'o','r','c','a'},{'r','a','i','o'},{'a','t','n','s'}};
   struct arv teste;
   arvpre a;
   a=&teste;
   for (i=0;i<dimy;i++)
      for (j=0;j<dimx;j++){
         c=23;
         for (b=0;b<23;b++) printf("%d %d ",vis[b].y,vis[b].x);
         printf("\n");
         a=criararvore(dimy,dimx,sopal,i,j,&amp;c,vis,k);
         printf("Sucssess for %c! \n", a->valor);
         for (b=0;b<23;b++) printf("%d %d ",vis[b].y,vis[b].x);
         printf("\n");
      }

   return 0;
}

The program will seg-faults, however the array is printed a few times before that. It changes each time it is called.

Why is this?

PAR vis[] in a parameter list is exactly the same as PAR* vis .

Any assignment through vis , like vis [0].x = 1; will change the parameters that you passed in the calling function.

PAS is passed by pointer (array, but makes really no difference since it's the same thing in this case), meaning that the function has the access to the exact same variable. Passing by pointer means that only the address of that parameter is passed and is a convenient way to alter a parameter in a separate function.

If you would pass it by value - another way of passing data in C, a new local instance of that variable would be created and would only have scope until the end of that function. However the changes made to that parameter would not be visible outside.

There is another way of passing data not available in C - passing by reference. It it somewhat 'safer' way of passing parameters then by pointer because a pointer can receive a NULL parameter, a reference parameter can not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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