0
#include <stdlib.h>
#include <stdio.h>
struct CELL {
    char* data;
    struct CELL* next;
} ;

struct CELL* table;

// The line below gives an error
table = malloc(SIZE * sizeof(struct HASHCELL));

I am trying to create an array of struct CELL using malloc.

However, doing so gives "warning: type specifier missing, defaults to 'int' [-Wimplicit-int]". Consequently, it gives "error: redefinition of 'table' with a different type: 'int' vs 'struct CELL * ". I am new to C and I am unsure why this is happening.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
user4672
  • 9
  • 2

2 Answers2

1

table = malloc(SIZE * sizeof(struct HASHCELL)); is a statement (it is an expression statement). Statements cannot be written outside of functions. Outside of functions, you can only have declarations and preprocessing directives. (In the C standard, declarations are separate from statements.)

When the compiler sees table, it is expect a declaration, but this is not a declaration because there is no type, like int table or struct Cell table. The compiler is applying a default type of int and warning you about this.

Then, because you had an earlier declaration struct CELL *table;, this new “declaration” of int table conflicts with the prior declaration, so the compiler warns you about that.

The solution is to write a function, such as the main function for your program, and put table = malloc(SIZE * sizeof(struct HASHCELL)); inside it.

You may also wish to move struct CELL *table; inside the function; using variables defined outside of functions is generally bad practice.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
-3

Make sure you closed the semicolon at the end of your structure definition.

struct CELL {
    char *data;
    struct CELL* next;
};

Make sure you include <stdlib.h> standard library where malloc() is defined before you call it.

Cast the return type of malloc to

table = (struct CELL*)malloc(100 * sizeof(struct CELL));

as malloc always returns void * to the allocated memory.

Compile your source code using command line gcc to make sure you don't have any library configuration issues in vs code which may cause the issue.

Vahan
  • 166
  • 13
  • 5
    C does not require casts of `void *` in assignments; it will automatically be converted to the destination type, and using casts can sometimes obscure errors. – Eric Postpischil Feb 27 '21 at 11:44
  • 1
    This is rather a comment than an answer. – Jabberwocky Feb 27 '21 at 11:45
  • Agreed with the above comments , OP's code already has everything mentioned here, and the cast is implicit . The second part , is probably useful but more of a comment too, and if I might add to it, I'd ask that he compile with all warnings – An Ant Feb 27 '21 at 11:47
  • 2
    In C you really [shouldn't cast the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – Some programmer dude Feb 27 '21 at 11:48
  • @Someprogrammerdude brilliant, this is interesting – An Ant Feb 27 '21 at 11:49