简体   繁体   English

CS50 [一般 - 但与 PSET4 恢复相关] 为什么 clang 说我的标识符未为函数声明?

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

I created a function called write (placed at the bottom after main) that calls several variables.我创建了一个叫做 write 的函数(放在 main 之后的底部),它调用了几个变量。 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. Write 被调用(初始化? - 抱歉,我仍然不确定关于这个的术语)这些变量都是在调用 main 函数之前声明的。 Despite this, when I run clang for the function, it says that all the variables in the function write are undeclared.尽管如此,当我为该函数运行 clang 时,它说函数 write 中的所有变量都未声明。

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.我能够通过从 main 复制所有已声明的变量并将其移动到 main 之上以使程序工作来修复它,但我很好奇为什么原始代码一直说变量未被识别。 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?对我来说,有没有办法保留在 main 函数中声明的变量,而不是将它们移到 main 之上以使其继续工作?

these are variables that kept getting marked as undeclared while they were in main这些变量在处于 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下面是在将这些变量移动到 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);
}

Your variables are declared in main so they are not available to write() .您的变量在main中声明,因此它们不可用于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).您需要将它们作为参数传递给write()或在main之外声明它们以使它们全局化(尽管这通常不是一个好习惯)。

You probably should learn a bit more about scope .您可能应该更多地了解scope

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM