tags:

views:

154

answers:

6
#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

I know how to make it work,but I'm asking why it DOESN'T work.

+2  A: 

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'.

thyrgle
+4  A: 

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; 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).

Jerry Coffin
+1, and compilers certainly have some rather oddball errors for this one. `clang` and `gcc` do not give very accurate errors for this problem.
birryree
Why can't `top= ` be regarded as initializing for `top`?
justnobody
Why this works: `int i;i = 100;` outside `main`?
justnobody
@justnobody see either mine or Chris Dodd's answers further down for an explanation of why that code works. You are actually declaring a variable named `i` twice, just that the second one has an implicit, default type of `int`.
birryree
A: 

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;
alpha123
A: 

Another option:

#include <stdio.h>

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

void main()
{
    printf("%d\n",*top);
}
Mark Tolonen
+1  A: 

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; is that you are actually re-declaring a variable called top, and it defaults to the int type. 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.

In C, variables that are declared without a type default to int, and that would generate the error you see.

error C2440: 'initializing' : cannot convert from 'int *' to 'int'

What this is really complaining about is that top = &a; as in your code actually looks like int top = &a; to the compiler, so you are trying to bind an int* to int.

birryree
+5  A: 

You're actually tripping over the compiler's support for ancient C syntax. The original C compiler allowed declarations without a type, defaulting it to int. So outside of any function,

foo;

would declare a global int variable called foo. So when you say

top = &a;

it declares a global int variable called top and tries to initialize it with the address of a. This gives you the two errors you see -- two conflicting declarations for top and an inability to convert an int * to an int. Of course those same ancient C compilers would not give you the second error, happily converting the address to an int without complaint.

This also tells you why 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)

There's lots of fascinating stuff in Dennis Ritchie's The Development of the C Language

Chris Dodd
What good timing Chris Dodd. :)
birryree