Because of this error: "return makes pointer from integer without a cast"

2
int *aloca_vetor(int MAX){
int i,*vetor;
for ( i = 0; i < MAX; ++i) {
    vetor[i] = (int*) calloc (1, sizeof(int));
}
return (vetor);

Error:

projeto.c:13:15: warning: assignment makes integer from pointer without a cast [enabled by default]
     vetor = (int*) calloc (1, sizeof(int));

I do not know what's causing this error.

    
asked by anonymous 09.08.2014 / 21:54

1 answer

4

Let's hit the nomenclature first. It is not giving an error, it is a warning, a nonconformity warning that normally the compiler can pass, but it can cause problems ... in your case it will not cause. At least not because of the warning .

Explaining warning :

You have created a pointer to int ( int i, * vetor; ). When you access a pointer with the indexer, it "resolves" the pointer to its type, so you're trying to put a int* type inside a int variable.

Why did I say that this will not cause a problem?

  • Because int has the same size in memory as int*

As I understand it from your code, you are trying to do a routine that returns a pointer to the allocated vector of type int with size X ... your routine has a number of conceptual problems:

You should do something like this:

int* aloca_vetor(int tamanho) {
  return (int*)malloc( tamanho * sizeof(int) );
}

And then give a free on the returned pointer when it will no longer be used.

    
09.08.2014 / 23:42