0

I'm pretty sure I'm using pointers incorrectly but I don't fully understand why using them in this way is wrong. The compiler seems to throwing out an error on the line (near the bottom of the code) where I try work out the size of the image (the error being: error: invalid operands to binary expression ('int *' and 'LONG' (aka 'int'))) and I have no idea why it's doing this or how to fix it properly. Any help would be great! Also sorry about the amount of code...

//Assigning memory to pointers
int* scaler = malloc(sizeof(int));
char* infile = malloc(sizeof(char)*strlen(argv[2]) + 1);
char* outfile = malloc(sizeof(char)*strlen(argv[3]) + 1);
int* padding = malloc(sizeof(int));

*scaler = atoi(argv[1]);
infile = argv[2];
outfile = argv[3]; 


FILE* input = fopen(infile, "r");
if(input == NULL){
    printf("Error could not connect to %s\n", infile);
    fclose(input);

}
FILE* output = fopen(outfile, "w");

if(output == NULL){
    printf("Error could not connect to %s\n", outfile);
    fclose(output);
}

//assigning memory for the input file's data
BITMAPFILEHEADER* bfInput = malloc(sizeof(BITMAPFILEHEADER));
BITMAPINFOHEADER* biInput = malloc(sizeof(BITMAPINFOHEADER));

fread(bfInput, sizeof(BITMAPFILEHEADER), 1, input);
fread(biInput, sizeof(BITMAPINFOHEADER), 1, input);

//assigning memory for the output file's data
BITMAPFILEHEADER* bfOutput = malloc(sizeof(BITMAPFILEHEADER));
BITMAPINFOHEADER* biOutput = malloc(sizeof(BITMAPINFOHEADER));

  if (bfInput->bfType != 0x4d42 || bfInput->bfOffBits != 54 || biInput->biSize != 40 || biInput->biBitCount != 24 || biInput->biCompression != 0){
    fclose(output);
    fclose(input);
    fprintf(stderr, "Unsupported file format.\n");
    return 4;
}


//copies the data from the input file to the output file 
   memcpy(bfOutput, bfInput, sizeof(BITMAPFILEHEADER));
   memcpy(biOutput, biInput, sizeof(BITMAPINFOHEADER));

   //works out the width of the new BMP
   biOutput->biWidth *= *scaler;

   //works out padding 
   *padding = ((4 -biOutput->biWidth * sizeof(RGBTRIPLE))%4)%4;

   //works out the height of the new BMP
   biOutput->biHeight *= *scaler;

   biOutput->biSize *=  sizeof(RGBTRIPLE) + padding * biOutput->biHeight;


   //writes the new information the output files headers
   fwrite(bfOutput, sizeof(BITMAPFILEHEADER), 1, output);
   fwrite(biOutput, sizeof(BITMAPINFOHEADER), 1, output);

1 Answer 1

0

It seems to me that the problem that is causing this error is located where you calculate the new biSize, since you just declared int* padding to be a pointer, you are multiplying a pointer with an integer.

I think the your code for biSize should be like this:

biOutput->biSize *=  sizeof(RGBTRIPLE) + *padding * biOutput->biHeight;

This doesn't mean that the calculation is correct.

1
  • Ahh right cool, Thanks!
    – Kareem
    Jul 26, 2015 at 18:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .