Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
访问冲突读取位置0x000000xx-c结构属性-Visual Studio_C_Visual Studio 2008_Struct_Access Violation - Fatal编程技术网

访问冲突读取位置0x000000xx-c结构属性-Visual Studio

访问冲突读取位置0x000000xx-c结构属性-Visual Studio,c,visual-studio-2008,struct,access-violation,C,Visual Studio 2008,Struct,Access Violation,正如标题所讲述的故事。下面粘贴的是一段代码和所有依赖项。这个错误对我来说很奇怪,因为我可以在监视窗口中访问相同的变量,并且我找不到在运行时出现此错误的有效原因。我能找到的大多数标题相同的问题都与访问的空地址有关,这里的情况并非如此。0X00000069是无法访问的位置。我还附上了下面的观察窗口截图 typedef struct { void * variable; // variable address char * signature; char name[3

正如标题所讲述的故事。下面粘贴的是一段代码和所有依赖项。这个错误对我来说很奇怪,因为我可以在监视窗口中访问相同的变量,并且我找不到在运行时出现此错误的有效原因。我能找到的大多数标题相同的问题都与访问的空地址有关,这里的情况并非如此。0X00000069是无法访问的位置。我还附上了下面的观察窗口截图

typedef struct
{
    void  * variable;     // variable address
    char * signature;
    char name[30];
    char type[50];
}   GlobalVariable;

GlobalVariable VariableTable[] = 
{
    { &a, "int a = 7;", "a", "int"},
    { &ch, "char ch = 'a';", "ch", "char"},
    { NULL, NULL }
};

void PrintGlobalVariables()
{
    GlobalVariable * variable = VariableTable;
    int count = 0;
    while(variable->variable)
    {
        count++;
        variable++;
    }
    if(!count)
    {
        OutputDebugString("No global variables passed.\n");
        return;
    }
    int i = 0 ;
    variable = VariableTable;

    char       temp[MAX_PATH];
    strcpy(temp, "                                                             ");
    OutputDebugString("Global Variables = "); 
    while(variable->variable)
    {
        if(strcmp(trimwhitespace(variable->type),"int") == 0)
        {


            ////////THIS IS THE STATEMENT BELOW WHERE I AM GETTING ERROR
            wsprintf(temp, "%s=%ld",*(char *)variable->signature, *(long *)variable->variable);


        } else
        if(strcmp(trimwhitespace(variable->type),"char") == 0)
        {
            wsprintf(temp, "%s=%c", *(char *)variable->name, *(char *)variable->variable);
        }
        if((variable+1)->variable != NULL)
        {
            wsprintf(temp + strlen(temp), ",", "");
        }
        OutputDebugString(temp);
        variable++;

    }
    OutputDebugString("\n");
}
char *trimwhitespace(char *str)
{
    char *end;

    // Trim leading space
    while(isspace(*str)) str++;

    if(*str == 0)  // All spaces?
        return str;

    // Trim trailing space
    end = str + strlen(str) - 1;
    while(end > str && isspace(*end)) end--;

    // Write new null terminator
    *(end+1) = '\0';
    return str;
}

签名
字段是一个
字符*
。取消对它的引用将产生一个
字符
。但是
%s
格式说明符需要
字符*
。解决方法是通过删除取消引用来更改该行(而且也不需要强制转换)


请发布一个完整的程序,我们可以自己编译和运行,并观看崩溃。对于这样的bug,根本原因几乎总是远离问题出现的地方。如果可能,还可以删除所有非ISO C库函数的使用(例如,使用
printf
puts
替换对
OutputDebugString
的所有调用)。如果使用sane编译器编译此函数,您将获得格式字符串检查的乐趣,这会立即向您显示错误。@EOF我没有收到您的评论。@TahaRehmanSiddiqui:当您开始随机添加强制转换和间接运算符时,一个好的编译器会对您大喊大叫。它怎么能给您一个字符串<代码>签名是一个
字符*
。因此,如果您取消引用它,您将得到一个
char
而不是字符串。这甚至显示在你的手表窗口里。因此,
wsprintf
当然会崩溃。
wsprintf(temp, "%s=%ld",*(char *)variable->signature, *(long *)variable->variable);
wsprintf(temp, "%s=%ld", variable->signature, *(long *)variable->variable);