简体   繁体   English

为什么以下代码失败

[英]Why this code below fails

#include <stdio.h>

int *top;
int a=1;
top=&a;

void main()
{
    printf("%d\n",*top);
}
error C2440: 'initializing' : cannot convert from 'int *' to 'int'

UPDATE UPDATE

I know how to make it work,but I'm asking why it DOESN'T work. 我知道如何使它工作,但我问为什么它不起作用。

You're actually tripping over the compiler's support for ancient C syntax. 你实际上是在嘲笑编译器对古老C语法的支持。 The original C compiler allowed declarations without a type, defaulting it to int. 原始C编译器允许没有类型的声明,将其默认为int。 So outside of any function, 所以在任何功能之外,

foo;

would declare a global int variable called foo . 将声明一个名为foo的全局int变量。 So when you say 所以当你说

top = &a;

it declares a global int variable called top and tries to initialize it with the address of a . 它声明了一个全局int变量所谓的top ,并试图用的地址初始化a This gives you the two errors you see -- two conflicting declarations for top and an inability to convert an int * to an int . 这给出了你看到的两个错误 - top两个冲突声明和无法将int *转换为int Of course those same ancient C compilers would not give you the second error, happily converting the address to an int without complaint. 当然,那些相同的古老C编译器不会给你第二个错误,愉快地将地址转换为int而没有抱怨。

This also tells you why int i; i = 100; 这也告诉你为什么int i; i = 100; int i; i = 100; works --- its two declarations for i as a global int variable (which is ok, as they're the same type), and the second initializes it to 100 (which is ok as only one declaration has an initializer) 工作---它的两个声明为i作为全局int变量(这是好的,因为它们是相同的类型),第二个将它初始化为100 (这是好的,因为只有一个声明有一个初始化器)

There's lots of fascinating stuff in Dennis Ritchie's The Development of the C Language Dennis Ritchie的“C语言的发展”中有很多有趣的东西

I'm a bit surprised at the exact error message you got about it, but the problem (or at least one of the obvious problems) is that outside of a function, you're allowed to define and initialize variables, but you're not allowed to do a normal assignment -- that has to be done as part of executing a function. 我对你得到的确切错误信息感到有些惊讶,但问题(或至少有一个明显的问题)是在函数之外,你可以定义和初始化变量,但你是不允许做一个正常的分配-这必须做作为执行功能的一部分。 As such, your top=&a; 因此,你的top=&a; isn't allowed. 是不允许的。

Another problem is that you have main returning void , where it should return an int (though most compilers will accept that without even a warning, not to mention an error message). 另一个问题是你有main返回void ,它应该返回一个int (尽管大多数编译器都会接受它,甚至没有警告,更不用说错误信息了)。

Actually, from your original code: 实际上,从您的原始代码:

int *top;
int a=1;
top=&a;

As others have mentioned - in global space you can declare, or declare and initialize. 正如其他人所提到的 - 在全球空间中,您可以声明,声明和初始化。 You can not do assignment. 你不能做任务。

What is actually happening in the line top = &a; 实际上在行top = &a;发生了什么top = &a; is that you are actually re-declaring a variable called top , and it defaults to the int type. 你实际上是在重新声明一个名为top的变量,它默认为int类型。 The compiler should actually warn about creating a new variable named top that has the same name as a previously declared variable, as well as generate an additional warning that you are creating a variable of default type int . 编译器实际上应警告创建一个名为top的新变量,该变量与先前声明的变量具有相同的名称,并生成另一个警告,表明您正在创建一个默认类型为int的变量。

In C, variables that are declared without a type default to int , and that would generate the error you see. 在C中,声明没有类型的变量默认为int ,并且会生成您看到的错误。

error C2440: 'initializing' : cannot convert from 'int *' to 'int' 错误C2440:'初始化':无法从'int *'转换为'int'

What this is really complaining about is that top = &a; 真正抱怨的是top = &a; as in your code actually looks like int top = &a; 在你的代码实际上看起来像int top = &a; to the compiler, so you are trying to bind an int* to int . 到编译器,所以你试图将int*绑定到int

This works: 这有效:

#include <stdio.h>

int *top;
int a=1;
int main()
{
    top=&a;
    printf("%d\n",*top);
}

You need to be sure not do global assignment like that. 你需要确保不要做那样的全局分配。 You can only do initialization, nothing else for global variables. 您只能进行初始化,没有其他全局变量。

You also have not mentioned the other errors, please mention all errors, sometimes one error is based of another and when one is fixed the others get fixed. 您还没有提到其他错误,请提及所有错误,有时一个错误是基于另一个错误,当一个错误修复时,其他错误得到修复。 In this case it was: 在这种情况下,它是:

Conflicting types for 'top'. 'top'的冲突类型。

I honestly have no idea why that fails, but this works: 老实说,我不知道为什么会失败,但这有效:

#include <stdio.h>

int a = 1;
int* top = &a;

void main()
{
    printf("%d\n", *top);
}

And remember: ALWAYS INITIALIZE YOUR POINTERS!! 并记住:总是初始化你的指针! If you're not going to assign them immediately, at least do something like this: 如果你不打算立即分配它们,至少做这样的事情:

int* top = 0;

Another option: 另外一个选项:

#include <stdio.h>

int a=1;
int *top=&a;

void main()
{
    printf("%d\n",*top);
}

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

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