incompatible types when assigning to type 'int *[]' from type 'int'

Asked

Viewed 2,690 times

2

I created a size vector defined by a constant, and when calling a function that returns me an integer vector, this error is generated:

incompatible types when assigning to type 'int *[]' from type 'int'

The signature of Function is this below. The intention is to assign the return of one vector to another.

#include <stdlib.h>
#include <stdio.h>
#define TAM 50

int main(int argc, char **argv)
{
    int *V_VALORES_GERADOS[TAM];
    int *VETOR_ORDENADO[TAM];
    int *VETOR_VALORES[TAM];
    int *V_FREQ_SIMP_ABS[TAM];
    float *V_FREQ_SIMP_REL[TAM];
    int *V_FREQ_ACUM_ABS[TAM];
    float *V_FREQ_SIMP_ACUM[TAM];
    V_VALORES_GERADOS = Gerar_Vetor(6); //ERRO AQUI(PRIMEIRA INSTRUÇÃO DA MAIN)
}

int *Gerar_Vetor(int FATOR){    
    int x;
    int * VALORES = (int *) calloc (TAM, sizeof (int)); 

    //Gera os valores e os armazena em um vetor. 
    for (x = 1;x <= TAM; x++) {   
        VALORES[x] = rand() % FATOR + 1;
    }
    if (VALORES == NULL)
        return (NULL);
    else
        return VALORES;
}
  • With int *V_VALORES_GERADOS[TAM]; you are setting an array of pointers to integers. Is that right? Wouldn’t it be an array of integers? Or better just a pointer to the beginning of the allocated area?

2 answers

1


With this construction :

int *V_VALORES_GERADOS[TAM];

you are saying that V_VALORES_GERADOS is an array of pointers for integers.
Your routine returns a pointer to integer array.

See the changes I made to your code:

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int *Gerar_Vetor(int FATOR);
#define TAM 50

int main(int argc, char* argv[])
{
    int * P_V_VALORES_GERADOS = NULL;
    //inicializa semente 
    srand( (unsigned)time( NULL ) );
    P_V_VALORES_GERADOS = Gerar_Vetor(6); //ERRO AQUI(PRIMEIRA INSTRUÇÃO DA MAIN)
    return 0;
}

int *Gerar_Vetor(int FATOR)
{    
    int x = 0;//sempre inicialize as variáveis
    int * VALORES = (int *) calloc (TAM, sizeof (int)); 
    //aqui você testa se a alocação teve êxito, pois se não fizer isto a execução seguinte pode causar erros em seu programa.
    //altera para printar na tela o motivo do erro.
    if (VALORES == NULL)
        return (NULL);
    //Gera os valores e os armazena em um vetor. 
    for (x = 1;x <= TAM; x++) 
    {   
        VALORES[x] = rand() % FATOR + 1;
    }
    return VALORES;
}

0

My suggestion is to pass an array parameter and put the data in it.

void function(int *vetor){
     vetor = (int*) malloc(sizeof(int));
     vetor[0] = 1;
}

Browser other questions tagged

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