0

I'm receiving a segfault when reading the first byte (and probably would on the following) of buffer which is a malloced array -- or perhaps not, if unsigned char * doesn't mean what I think it does. Either way, this should be the relevant code;

char * name(); and FILE* outptr = NULL; is declared above main, globally. BLOCK and infile is #defined as 512 and "card.raw" respectively.

The rest of int main(void) is below.

// Open input file, plus error check
FILE* inptr = fopen(infile, "r");
if (inptr == NULL)
{
    printf("Could not open %s.\n", infile);
    return 3;
}


// Allocate memory for one BLOCK and read from infile
unsigned char * buffer = malloc(BLOCK);
    if (buffer == NULL)
        return 3;

while (fread(&buffer, BLOCK, 1, inptr) != 0) {

    */ REMAINDER OF CODE REDACTED TO COMPLY WITH ACADEMIC HONESTY OF /*
}

Thanks!

1 Answer 1

2

What I think is going on is:

When you declare unsigned char* buffer you are creating a pointer, which has the value of an address in memory. When you pass &buffer to fread, the ampersand represents the address of your pointer buffer (another pointer). Then fread will try to read its contents into the space where the buffer pointer is in memory, which only fits an address value.

Hope this helps!

You must log in to answer this question.

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