首页 > 解决方案 > 为什么在注释行后看不到初始化变量?

问题描述

错误信息是:error: 'basicInfo' undeclared (first use in this function) basicInfo->errorlog = malloc(VAL_SZ);

#include <stdio.h>
#include <stdlib.h> //malloc
#include <string.h> //strdup

#define VAL_SZ 64

typedef struct {
    char* errorlog;
    char* cache_size;
    char* cache_replacment;
    int timeout;
} basicInfo_t;

int main(int argc, char* argv[]) {

    //-------------BASIC INFO SETUP-------------\\
    basicInfo_t* basicInfo = malloc(sizeof(basicInfo_t));
    basicInfo->errorlog = malloc(VAL_SZ);
    basicInfo->cache_size = malloc(VAL_SZ);
    basicInfo->cache_replacment = malloc(VAL_SZ);

    return 0;
}

标签: cstructcompiler-errorstypedef

解决方案


在您的评论中,您\\最后使用了 a

//-------------BASIC INFO SETUP-------------\\

告诉编译器将语句继续到下一行。因此,线

basicInfo_t* basicInfo = malloc(sizeof(basicInfo_t));

实际上对编译器不可见(=注释掉)。

删除\\它应该可以工作。


推荐阅读