Segmentation failure in C language

Asked

Viewed 581 times

0

Hi, I have a problem... The code only runs until a part after this appears:

Falha de segmentação (imagem do núcleo gravada)

Follow my program below:

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

int seleciona_indice (int array[], int tamanho)
{
    int i, rnd = rand() % tamanho;

    for (i = rnd + 1; i < tamanho; i++)
        array[i - 1] = array[i];

    return rnd;
}

void embaralhar (char *array[], int tamanho)
{
    int i;
    char *aux;
    int *listaIndices = malloc (tamanho * sizeof (int));

    srand (time (0));
    for (i = 0; i < tamanho; i++)
    listaIndices[i] = i;

    for (i = 0; i < tamanho - 1; i += 2)
    {
        int indiceA = seleciona_indice (listaIndices, tamanho - i);
        int indiceB = seleciona_indice (listaIndices, tamanho - i - 1);

        aux = array[indiceA];
        array[indiceA] = array[indiceB];
        array[indiceB] = aux;
        }
    free (listaIndices);
}

int main(void)
{

  char nome[23];
  int *data;
  printf("==============================\n");

  printf("          PARSENHA\n");

  printf("==============================\n");

  printf("Digite seu nome: ");
  scanf("%s", nome);
  printf("Digite sua data de nascimento: ");
  scanf("%d", data);
  embaralhar(nome, 23);
  printf("Senha forte: %s%ls" , nome, data);

  return 0;

}

1 answer

2

The gcc generates several warnings when you compile your code. Check warnings and fix them. But the error is occurring because you are trying to use an invalid pointer:

int *data;

in

scanf("%d", data);

It is right to declare data as int:

int data;

and use:

scanf("%d", &data);
  • thanks! but I remember I got an error of char pointers. can help me with this error too?

  • I can’t understand what your code is trying to do.

Browser other questions tagged

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