1

I'm working with dynamic arrays and this is the declaration:

int *vetor = (int *) malloc (tam*sizeof(int));

vetorAleatorio (vetor, tam); //chamando função abaixo

but when I try to pass it as parameter to this function:

void vetorAleatorio(int **vet, int size) {
 int i;

 for (i=0; i<size; i++)
       vet[i] = rand() %1000;}

I have the following errors:

[Warning] assignment makes pointer from integer without a cast
[Warning] passing arg 1 of `vetorAleatorio' from incompatible pointer type 

Someone know how this is happening?

simonc
  • 41,632
  • 12
  • 85
  • 103
user2623414
  • 19
  • 1
  • 2
  • 3
  • 2
    This is just a comment, but avoid casting malloc in C [source](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858) – CBIII Jul 26 '13 at 15:52
  • Usually, you have a double pointer argument (**vet) if you are going to allocate within the function and make use of it outside the function. int *vetor = Null; vetorAlea(&vetor, tam); //allocated in function.... – Jiminion Jul 26 '13 at 16:00
  • but you MUST cast malloc in C++ Source..... (sigh...) – Jiminion Jul 26 '13 at 16:01

5 Answers5

5

Your function syntax:

void vetorAleatorio(int **vet, int size) 

should be:

void vetorAleatorio(int *vet, int size)
                        ^ 
                        // remove one *

[Warning] assignment makes pointer from integer without a cast

if you use double * as int ** for vet, then its type mismatch as follows:

vet[i] = rand() %1000
   ^        ^ 
   |          int  // rand() %1000 returns a int
 type is int* 
 // vet[i] ==  *(vet + i) == *(pointer to pointer of int) = pointer int = int*

Warning-2: passing arg 1 of `vetorAleatorio' from incompatible pointer type

Understand In your code you according to void vetorAleatorio(int **vet, int size) declaration you are calling function in wrong way: vetorAleatorio (vetor, tam);, you are passing address of int = pointer to int, and argument need address of pointer to int = pointer to pointer to int.

You just need one rectification as I suggested above.

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
1

int **vet declares that the vet argument is a pointer to a pointer to int. i.e. an array of arrays of integers. It looks like you just want to pass a pointer to a single vector so you should declare the argument as type int* instead

void vetorAleatorio(int *vet, int size) {
simonc
  • 41,632
  • 12
  • 85
  • 103
0

Your function signature for vetorAleatorio is wrong - change:

void vetorAleatorio(int **vet, int size)

to:

void vetorAleatorio(int *vet, int size)

Also note that you should never cast the result of malloc in C, so change:

int *vetor = (int *) malloc (tam*sizeof(int));

to:

int *vetor = malloc (tam*sizeof(int));
Paul R
  • 208,748
  • 37
  • 389
  • 560
0

vetor is of type int *, where veteroAleatorio is expecting an int **

You should have

void vetorAleatorio(int *vet, int size) {
 int i;

 for (i=0; i<size; i++)
       vet[i] = rand() %1000;}
sedavidw
  • 11,116
  • 13
  • 61
  • 95
0

You have an extra *. This should work:

void vetorAleatorio(int *vet, int size)

You are passing in a pointer to an int (the variable vetor), so your function declaration should accept a pointer to an int.

jh314
  • 27,144
  • 16
  • 62
  • 82