Lisandra Melo
Lisandra Melo

Reputation: 15

C function: why my variables changing values right after setting them?

I'm building a program that is saving information on a matrix of Pixels (a struct). A pixel must have its color (another struct) the color is a struct with three integers (red=vermelho, green=verde and blue=azul). But when I set them their value change right after the end of the for. For example, if I put the value 250 on all the red attributes of pixels it somehow changes if I print them after the for is finished if I print them while the for is running the values are right.

I'm also reading the information of the value of the red, green and blue values of a file, but I printed them and they are right.

Bellow are the functions that I'm using, and I'm having problems in the function clear from it I'm getting "250 0 0" (the right value) for all the pixels inside the for but the values change being sometimes "250 0 0", "0 250 0" and "250 250 0" in the second for inside the clear function.

I've tried printing all the variables, but it seems they somehow magically change after I set them in the first for. I'm not really experienced at C so I'm really lost right now.

typedef struct cor{
    int vermelho;
    int verde;
    int azul;
} Cor;

typedef struct pixel{
    Cor cordopixel;
} Pixel;

typedef struct imagem{
    Pixel **pintura;
    int largura;
    int altura;
} Imagem;

void image(FILE *pont_arq, Imagem *desenho){
    int i, j, k;
    fscanf(pont_arq, "%d %d", &i, &j);
    desenho->altura = j; //value equals 400
    desenho->largura = i; //value equals 600
    printf("Cria imagem %dx%d\n", i, j);
    desenho->pintura = calloc(i, sizeof(Pixel*));
    for (k = 0; k < i; ++k){
        desenho->pintura[k] = calloc(j, sizeof(Pixel));
    }
    printf("Saiu\n");
}

int* lerIntArquivo(FILE *pont_arq, int inicio, int vezes, int val[]){
    if(vezes==inicio-1){
        fscanf(pont_arq, "%d", &val[inicio]);
        return val;
    }else{
        fscanf(pont_arq, "%d", &val[inicio]);
        lerIntArquivo(pont_arq, inicio+1,vezes, val);
    }
}

void clear(FILE *pont_arq, Imagem *desenho){
    int val[3], i, j;
    lerIntArquivo(pont_arq, 0, 3, val);
    printf("Cria imagem");
    printf("%d %d %d\n",val[0],val[1], val[2]); //val[0]=250 val[1]=0 val[2]=0
    //this for is printing the right values
    for (i = 0; i < desenho->altura; ++i){
        for (j = 0; j < desenho->largura; ++j){
            printf("%d %d\n",i,j);
            desenho->pintura[i][j].cordopixel.vermelho = val[0];//setting the values
            desenho->pintura[i][j].cordopixel.verde = val[1];//setting the values
            desenho->pintura[i][j].cordopixel.azul = val[2];//setting the values
            printf("%d ", desenho->pintura[i][j].cordopixel.vermelho);
            printf("%d ", desenho->pintura[i][j].cordopixel.verde);
            printf("%d\n", desenho->pintura[i][j].cordopixel.azul);

        }
    }
    //this for is not printing the right values
    for (i = 0; i < desenho->altura; ++i){
        for (j = 0; j < desenho->largura; ++j){
            printf("%d ", desenho->pintura[i][j].cordopixel.vermelho);
            printf("%d ", desenho->pintura[i][j].cordopixel.verde);
            printf("%d\n", desenho->pintura[i][j].cordopixel.azul);
        }

    }
    printf("\n");
}

There were no error messages, just wrong values.

Upvotes: 1

Views: 93

Answers (1)

AnT stands with Russia
AnT stands with Russia

Reputation: 320451

When you allocate memory for your image the value of largura is the first size of the 2D array and the value of altura is its second size

desenho->altura = j;
desenho->largura = i;
...
desenho->pintura = calloc(i, sizeof(Pixel*));
for (k = 0; k < i; ++k){
    desenho->pintura[k] = calloc(j, sizeof(Pixel));
}

I.e. you create an array of [largura][altura] size.

But when you latter work with the array you suddenly reverse their roles

for (i = 0; i < desenho->altura; ++i){
  for (j = 0; j < desenho->largura; ++j){
    ...
    desenho->pintura[i][j]

I.e. you access it as a [altura][largura] array, which is incorrect.

At some step you managed to swap the values. Most likely, when you allocated the image memory it was supposed to be

desenho->altura = i;
desenho->largura = j;

not the other way around.

Upvotes: 2

Related Questions