c - Why is this struct passed as argument of a function, change is values? -
i have struct , 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 can see, pass par vis
array of structs. i've been taught, way, value of vis
declared outside function; , therefore should remain unaltered, right? example, if it's 0 before calling function, after function should still zero. however, values change after calling function.
here 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; (i=0;i<dimy;i++) (j=0;j<dimx;j++){ c=23; (b=0;b<23;b++) printf("%d %d ",vis[b].y,vis[b].x); printf("\n"); a=criararvore(dimy,dimx,sopal,i,j,&c,vis,k); printf("sucssess %c! \n", a->valor); (b=0;b<23;b++) printf("%d %d ",vis[b].y,vis[b].x); printf("\n"); } return 0; }
the program seg-faults, array printed few times before that. changes each time called.
why this?
par vis[]
in parameter list same par* vis
.
any assignment through vis
, vis [0].x = 1;
change parameters passed in calling function.
Comments
Post a Comment