0

I'm having trouble compiling my recover.c code as the compiler is telling me that I'm trying to compare an integer to a pointer? This is referring to the check to see if the first characters in the block contain a jpeg signature.

I get that char* is a pointer, and that it points to the address of the first position in a character array, but when I'm making the comparison later on in the code, I'm comparing to the value in that position in the array, no?

I'm aware that there's probably many more errors in the code, as I'm struggling to get to grips with this pset, so any other nudges in the right direction would be very helpful.

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    FILE* file = fopen("card.raw", "r");
    if (file == NULL)
    {
        printf("Could not open file.\n");
        return 1;
    }

    int jpgnum = 0;
    int found = 0;
    char* filename = NULL;
    char* buffer[512];
    FILE* output = NULL;

    do
    {
        fread(buffer, sizeof(char), 512, file);

        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff &&
            buffer[3] >= 0xe0 && buffer[3] <= 0xef)
        {
            found = 1;
            if (output != NULL)
            {
                fclose(output);
                jpgnum++;
            }
            sprintf(filename, "%03i.jpg", jpgnum);
            output = fopen(filename, "w");
            if (output == NULL)
            {
                printf("Could not open file to be written.\n");
                return 2;
            }
            fwrite(buffer, sizeof(char), 512, output);
        }
        else if (found == 1)
        {
            fwrite(buffer, sizeof(char), 512, output);
        }
    }
    while (fread(&buffer, sizeof(char), 512, file) == 512);

    fclose(file);
    return 0;
}
1

The problem lies in your declaration of buffer:

char* buffer[512];

By inserting that asterisk, you didn't create a char array, you created an array of pointers to characters - in other words, an array of addresses. Your buffer should be an array of BYTEs. A BYTE, as you might remember from bmp.h is defined as an unsigned 8 bit integer (a uint8_t). Your buffer should hold 512 of these. In the IDE, an equivalent data type is an unsigned char so you could use this if you'd prefer.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

4
  • Changing the declaration to char resolved the error referring to a pointer, but it then gave a new error code for the same issue of "comparison of constant 255 with expression of type 'char' is always false". This leads me to think I should be declaring buffer as an int and not a char?
    – A.Logan
    Nov 4 '16 at 23:52
  • Interesting. Declaring the buffer type as char used to work, but no longer. I'll have to look into that. However, uint8_t will work instead of char. Also, you are going to have a problem with filename. It is again a pointer to a char, it's set to NULL and there's no memory allocated to it. There may be other problems, but that's for another question.
    – Cliff B
    Nov 5 '16 at 0:19
  • Thank you, kiwi, for the edit. ;-) It turns out that the makefile and/or compile flags for this problem have been updated to be more restrictive since I first did this pset. While char worked before, it is no longer valid. The buffer must now be an unsigned type.
    – Cliff B
    Nov 5 '16 at 1:42
  • That's solved that issue, guys! I created a typedef of BYTE for uint8_t and replaced the char with BYTE and that seems to have done the trick! However my code only outputs 25 files and they are all corrupted, so another problem to solve now!
    – A.Logan
    Nov 5 '16 at 12:18

You must log in to answer this question.

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