Malloc does not work in C code

When trying to compile the code I get the following error messages:

Terminal error message

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

#define OK 0

void preencherVetor(int* vetor[], int tamanho)
{

  int indice;

  for (indice = 0; indice < tamanho; indice++)
  {
    printf("Entre com o valor[%d]\n", indice+1);
    scanf("%d", vetor[indice]);
  }
}

void copiarVetor(int* novoVetor[], int* antigoVetor[], int tamanho)
{
  int indice;

  for (indice = 0; indice < tamanho; indice++)
  {
    (*novoVetor[indice]) = (*antigoVetor[indice]);
  }
}

int* somaVetores(int* vetorSoma[]/*, int tamanhoI, int tamanhoII*/)
{
  vetorSoma = (int*) malloc(sizeof (int) * 20);

  int indice = 0;
  int tamanho;
  int tamanhoI;
  int tamanhoII;
  int tamanhoExcedente; //Se os vetores possuírem tamanhos diferentes, o tamanhoExcedente será o tamanho do maior vetor
  int* maiorVetor[20];

  maiorVetor = (int*) malloc(sizeof (int) * 20);

  printf("\nDigite o tamanho do primeiro vetor\n(O valor do tamanho deve pertencer ao intervalo [1,20])\n");
  scanf("%d", &tamanhoI);

  printf("Digite o tamanho do segundo vetor\n(O valor do tamanho deve pertencer ao intervalo [1,20]\n");
  scanf("%d", &tamanhoII);

  int* vetorI[tamanhoI];
  vetorI = (int*) malloc(sizeof (int) * tamanhoI);

  int* vetorII[tamanhoII];
  vetorII = (int*) malloc(sizeof (int) * tamanhoII);

  printf("\nPreencha o primeiro vetor\n");

  preencherVetor(vetorI, tamanhoI);

  printf("\nPreencha o segundo vetor\n");

  preencherVetor(vetorII, tamanhoII);

  while ((tamanhoI >= 1) && (tamanhoI <= 20) && (tamanhoII >= 1) && (tamanhoII <= 20))
  {
    if (tamanhoI < tamanhoII)
    {
      tamanho = tamanhoI;
      tamanhoExcedente = tamanhoII;
      copiarVetor(maiorVetor, vetorII, tamanhoII);
    }

    else if ((tamanhoI) > (tamanhoII))
    {
      tamanho = tamanhoII;
      tamanhoExcedente = tamanhoI;
      copiarVetor(maiorVetor, vetorI, tamanhoI);
    }

    else
    {
      tamanho = tamanhoI; //Como neste caso os dois vetores possuem tamanhos iguais, *tamanho deve ser igual ao tamanho de qualquer um dos vetores
      tamanhoExcedente = tamanhoI;
      copiarVetor(maiorVetor, vetorI, tamanhoI);
    }

    for (indice = 0; indice < tamanho; indice++)
    {
      (*vetorSoma[indice]) = (int) ((*vetorI[indice]) + (*vetorII[indice]));
    }

    indice = tamanho;

    for (indice = tamanho; indice < tamanhoExcedente; indice++)
    {
      (*vetorSoma[indice]) = (*maiorVetor[indice]);
    }

    printf("\nVetor Soma = {");

    printf("%d", (*vetorSoma[0]));

    indice = 1;

    for (indice = 1; indice < tamanhoExcedente; indice++)
    {
      printf(", %d", (*vetorSoma[indice]));
    }

    printf("}\n");

    return *vetorSoma;
  }

  printf("ERRO\n O tamanho de um vetor excede o limite permitido!!!\n");

  return 0;
}



int main()
{
  int opcao;
  int* vetSoma;

  vetSoma = (int*) malloc(sizeof (int) * 20); //vetor soma

  do
  {
    printf("\nCALCULADORA DE VETORES E MATRIZES\n\n");
    printf("1 - Soma de dois vetores\n");
    printf("2 - Produto interno de dois vetores\n");
    printf("3 - Intercalação de dois vetores de 8 posições em um vetor de 16\n");
    printf("4 - Ordenação de um vetor de inteiros em ordem crescente\n");
    printf("5 - Transposta de uma matriz");
    printf("6 - Multiplicação de duas matrizes\n");
    printf("0 - Sair\n");
    scanf("%d", &opcao);

    switch (opcao)
    {
      case 1:
        somaVetores(&vetSoma/*, tamanho1, tamanho2*/);
        break;

      case 0:
        break;

      default:
        printf("Operação não disponível\n");
        break;
    }
  } while(opcao != 0);

    return OK;
}
Author: Maniero, 2017-05-16

1 answers

The first only generates a warning because it is only a possibility of problems, write like this and it should work:

int* vetSoma = malloc(sizeof (int) * 20);

The others cannot be used malloc() since they are arrays, they are allocated in the stack. Contrary to popular beliefarrays are not pointers . The same problem occurs as I had reported in the previous question . You are wanting to put a letter in the place where a house is expected.

Or transform these arrays in pointers, where you give, or do not use malloc(), which will make it impossible to use heap, then you have to see if it is what you need. If you are only using the stack you will have to make copies that may not be necessary.

After posting the code I saw that the first is like array also and therefore gives the same error, can not do this.

What are they and Where are the "stack" and "heap"?

 1
Author: Maniero, 2020-12-02 20:50:10