Search code examples
cscopedynamic-memory-allocationcallocglobal-scope

Error while declaring ptr using calloc in global scope


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


Solution

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