2

SPOILER ALERT THIS CODE ALREADY WORKS SO DON'T LOOK IF YOU'RE STILL SOLVING

I'm just wondering why my code still works even if I change fwrite(buffer, 512,1, img); to fwrite(&buffer, 512,1, img);

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

#define BUFFER_SIZE 512

int main(int argc, char* argv[]) {
    if (argc != 2){
        fprintf(stderr, "please lang umayos ka dalawa kelangan ko");
        return 1;
    }

    FILE *file = fopen(argv[1], "r");

    unsigned char buffer[BUFFER_SIZE];
    int files = 0; // how many files

    FILE* img = NULL; // write file here
    char filename[8];

    while (fread(buffer, BUFFER_SIZE,1, file) == 1 {
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xe0) == 0xe0) {
            if (files > 0) {
                fclose(img);
            }

            // create new file
            sprintf(filename, "%03d.jpg", files);
            img = fopen(filename,"a");
            files++;
        }

        // theres a file already so you can start
        if (files > 0) {
            fwrite(buffer, 512,1, img); // this is where I get confused
        }
    }
    fclose(img);
    fclose(file);

}
1
  • An interesting observation, and great learning point. Excellent question! – Tim Aug 9 '18 at 1:47
2

buffer is an array. Array variables "decay" into pointers when needed, so fwrite will understand buffer to mean a pointer to the first element (ie, the same as &buffer).

You must log in to answer this question.

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