0

I want to change the value of some struct variables inside of a function.

that's my declaration of my variables:

BMPCOLOR **tBMPImg;
BMPCOLOR **tBMPGrey;

that's my function call in the main function:

greyscale(tBMPImg, tBMPGrey, &tBMPInfoblock, &tBMPHeader);

Here you can see the function where i want to change the value of the variables:

void greyscale(BMPCOLOR ***tBMPImg, BMPCOLOR ***tBMPGrey, BITMAPINFORMATIONSBLOCK *tBMPInfoblock, BITMAPFILEHEADER *tBMPHeader) {      //Grau = 0,299*Rot + 0,587*Grün + 0,144*Blau
double dGrey;
int iX;
int iY;

FILE *fpGrey;
fpGrey=fopen("grau.bmp", "w+");

fwrite(tBMPHeader, sizeof(&tBMPHeader), 1, fpGrey);        //probably wrong too
fwrite(tBMPInfoblock, sizeof(&tBMPInfoblock), 1, fpGrey);

for(iX=0;iX<tBMPInfoblock->lbiWidth;iX++) {
    for(iY=0;iY<tBMPInfoblock->lbiHeight;iY++) {
        dGrey=0.299* tBMPImg[iY][iX]->cRed + 0.587 * tBMPImg[iY][iX]->cGreen + 0.144 * tBMPImg[iY][iX]->cBlue;  //Error: "error reading variable dGrey"
        tBMPGrey[iY][iX]->cRed=dGrey;                                                                           //i guess something is with those pointers wrong
        tBMPGrey[iY][iX]->cGreen=dGrey;
        tBMPGrey[iY][iX]->cBlue=dGrey;
    }
}
for(iX=0;iX<tBMPInfoblock->lbiWidth;iX++) {
    for(iY=0;iY<tBMPInfoblock->lbiHeight;iY++) {
        fputc(tBMPGrey[iY][iX]->cBlue,fpGrey);
        fputc(tBMPGrey[iY][iX]->cGreen,fpGrey);
        fputc(tBMPGrey[iY][iX]->cRed,fpGrey);
    }
    fwrite("0", tBMPInfoblock->lbiWidth % 4,1,fpGrey);
}

fclose(fpGrey);

I use the pointer to pointer to struct in the main, but i dont know how to handle it in another function. In other cases like: How to work with pointer to pointer to structure in C? was the solution (*foo)->member = 1;, but it didn't worked in my program.

I have read that i have to use pointer to pointer to pointer in that case. I guess the problem is that i just get the adress from the variables.

Community
  • 1
  • 1
whocares
  • 3
  • 2

1 Answers1

2

You don't need to add an additional layer of pointer indirection (i.e. another *) if you are accessing a data structure. You ONLY need to add another * if you are allocating memory and you need to send the new memory address back to the caller. Just use your existing declaration, like this, and replace the -> operators with . operators.

void greyscale(BMPCOLOR **tBMPImg, BMPCOLOR **tBMPGrey, ...
...

        dGrey=0.299* tBMPImg[iY][iX].cRed + 0.587 * tBMPImg[iY][iX].cGreen + 0.144 * tBMPImg[iY][iX].cBlue;
...

        tBMPGrey[iY][iX].cRed=dGrey;          

Unsolicited advice:

I should point out that your definition of BMPCOLOR **img is called a ragged or jagged image, and in most cases, that is NOT what you want for efficiency. What you want is

BMPCOLOR* img;

create the image with

img = malloc(NUM_COLUMNS * NUM_ROWS * sizeof(BMPCOLOR));

and access the pixels like

img[y * NUM_COLUMNS + x].cRed = 
Mark Lakata
  • 19,989
  • 5
  • 106
  • 123