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.
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 themalloc
?– Wakim