1

[cquery] type specifier missing, defaults to 'int' [-Wimplicit-int]
[cquery] redefinition of 'ptr' with a different type: 'int' vs 'int *

int *ptr,size=50;
ptr=(int*) calloc(size,sizeof(int));

How can i fix this error?Also what is the reason behind this.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 3
    You haven't really shown enough of your code for us to try to reproduce your problem. However if, as your title suggests, you have the `ptr=(int*) calloc(size,sizeof(int));` outside of *any* function, then that can't be done! Code that must execute **must** be inside a function (e.g. in `main`). – Adrian Mole Sep 23 '20 at 12:11
  • So do you mean that I cannot dynamically allocate a pointer in the global scope? – User 1426833 Sep 23 '20 at 12:15
  • 2
    That is correct! You can (statically) initialize variables at the global scope but using `calloc` requires actual *execution* of code - must be in a function body. – Adrian Mole Sep 23 '20 at 12:16

1 Answers1

1

For starters this

ptr=(int*) calloc(size,sizeof(int));

is not a declaration but an expression statement.

You may not place statements in a file scope.

Moreover a variable with the static storage duration (and file scope variables have the static storage duration) may be initialized with a compile-time constant.

So you should place the statement above in some function for example in main.

Also consider a possibility of redesigning your program such a way that it had as few file scope variables as possible.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335