This error tells: "Return makes Pointer from integer without a cast"

Asked

Viewed 9,629 times

3

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 don’t know what’s causing this mistake.

  • João, I have some questions: Are you doing a test, where do you want to allocate a non-sequential memory space for each item of this vector? Or are you just trying to allocate a vector the size of MAX? In addition, there is some reason not to use the malloc?

1 answer

4

Let’s get the nomenclature straight first. It is not giving error, it is a "Warning", warning of non-conformity that normally the compiler can pass, but can cause problems... in your case will not cause. At least not because of Warning.

Explaining the Warning:

You have created a pointer for int (int i, * vetor;). When you access a pointer by indicating the indexer, it "solves" the pointer to its type, so you’re trying to put a type int* within a variable int.

Because I said it won’t cause trouble?

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

From what I understand there of your code, you are trying to make 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 no longer using.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.