CS50 [general - but related to PSET4 Recover] Why does clang say my identifiers are undeclared for a function?

113 Views Asked by At

I created a function called write (placed at the bottom after main) that calls several variables. Write is called (initialized? - sorry I'm still not sure on the terminology on this) These variables are all declared prior to the call in the main function. Despite this, when I run clang for the function, it says that all the variables in the function write are undeclared.

I was able to fix it by copying all the declared variables in write from main and moving it to above main to get the program to work, but I was curious as to why the original code kept saying the variables were not identified. Conceptually, I don't fully get it. Is there a way for me the keep the variables declared in the main function as opposed to moving them above main to still get it to work?

these are variables that kept getting marked as undeclared while they were in main

int file_count = 0;
char filename[8];
bool first_jpg = false;
BYTE buffer[512];
FILE *photos = NULL;

Below is how the code looks prior to moving those variables above main

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

typedef uint8_t BYTE;


int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        printf("Usage: ./recover image\n");
        return 1;
    }
    
    //Open Memory Card
    FILE *card = fopen(argv[1], "r");
    int file_count = 0;
    char filename[8];
    bool first_jpg = false;
    BYTE buffer[512];
    FILE *photos = NULL;

    void write(void);
    //Read 512 into a buffer
    //Repeat until end of memory card
    while (fread(buffer, sizeof(BYTE), 512, card) == 512)
    {
        //If start of new jpg
        if (buffer[0] == 0xff && buffer[1] == 0xd8 && buffer[2] == 0xff && (buffer[3] & 0xf0) == 0xe0)
        {
            //if first jpeg
            if (!first_jpg)
            {
                //mark as first jpeg found
                first_jpg = true;
                write();
            }
            //not first jpg, meaning it is a new jpg
            else
            {
                //close old jpg file
                fclose(photos);
                //open and start writing new jpg
                write();
            }
            }
        else
        {
             //if already found jpeg
            if (first_jpg)
            {
                fwrite(buffer, sizeof(BYTE), 512, photos);
            }
            else
            //if no jpeg
            //continue reading into file until we find jpg file
            {
                continue;
            }
        }
    }

}

void write(void)
{
    sprintf(filename, "%03i.jpg", file_count);
    file_count++;
    photos = fopen(filename, "w");
    fwrite(buffer, sizeof(BYTE), 512, photos);
}
1

There are 1 best solutions below

0
On

Your variables are declared in main so they are not available to write(). You need to pass them as arguments to write() or declare them outside of main to make them global (even though this is usually not a good practice).

You probably should learn a bit more about scope.