Dynamic allocation in C

Asked

Viewed 60 times

1

I’m making a base of a hangman’s game.

I was trying to make a dynamic vector but it’s not working, it returns an error:

[Error] invalid Conversion from 'char*' to 'char'

Code:

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

int main(){

    char *palavras;
    char p[200], x;
    int cont,i;

    palavras = NULL;
    while (x!='S' && x!='s'){
        printf("\ndigite a palavra a ser inserida no jogo da forca: ");
        scanf("%s",p);
        cont++;
        palavras = (char *)realloc(palavras, sizeof(int)*cont);
        palavras[cont-1] = (char*)p;
        printf("Deseja sair? <S:N>: ");
        scanf("%s",&x); 
    }
    printf("saiu\n"); 
    printf("%s",p);
    for (i=0;i<cont;i++){
        printf("%s",palavras[i]);
    }
    getchar();
    return 0;
}
  • What do you want to do? This code makes no sense. If you do something that makes sense this error disappears.

1 answer

3

The first problem I see in your code is that x is not being initialized. Therefore, when testing while if the value is different s or S, the result is that the initial behavior is undefined. This is easy to solve, just initialize with n.

The second problem is that using scanf with %s without specifying the maximum size is equivalent to using a gets. The solution is to specify the maximum size with %199s. Another option is to use char p[201] and %200s. Note that the buffer has to be at least one byte larger than the maximum size of the scanf so that the null terminator of the string can also be stored.

A third problem is that the scanf("%s", &x); should only be scanf("%c", &x);. 'Cause here you just want to read a character.

A fourth problem is that you check yourself x has the value S or s which you consider as "Yes" and consider anything else as "No" when it should just be N and n.

But the big problem is the pointer palavras. Apparently you didn’t understand the concept of pointer and confused several things. A pointer to characters (char *) can point to an address that is the beginning of a string. This is something quite different than the list of words you are trying to create.

When you access palavras[i], since words are like char *, you will be accessing the letter in position i of a single word given by that pointer. This is quite different from your idea of accessing the word in the position i of the list.

Since you are accessing a string of characters thinking it is a list of words, the result is a compilation error, because the types of data expected by the compiler are not correct.

There are several different ways to implement a word list. On the face, I think of three ways to do this:

  1. Dynamically allocated lists of structured types where each node has the address of a string.

  2. Pointer to character pointer.

  3. Array of character pointers.

Let’s use approach 1:

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

typedef struct NODO_PALAVRA {
    char *conteudo;
    struct NODO_PALAVRA *proxima;
} NODO_PALAVRA;

typedef struct LISTA_PALAVRA {
    struct NODO_PALAVRA *primeira;
    struct NODO_PALAVRA *ultima;
} LISTA_PALAVRA;

void adicionar_palavra(LISTA_PALAVRA *lista, char *palavra) {
    NODO_PALAVRA *novo = (NODO_PALAVRA *) malloc(sizeof(NODO_PALAVRA));
    novo->proxima = NULL;
    novo->conteudo = (char *) malloc(201);
    strcpy(novo->conteudo, palavra);
    if (lista->primeira == NULL) {
        lista->primeira = novo;
        lista->ultima = novo;
    } else {
        lista->ultima->proxima = novo;
        lista->ultima = novo;
    }
}

void limpar_lista(LISTA_PALAVRA *lista) {
    while (lista->primeira) {
        NODO_PALAVRA *n = lista->primeira;
        lista->primeira = lista->primeira->proxima;
        free(n);
    }
    lista->ultima = NULL;
}

void mostrar_lista(LISTA_PALAVRA *lista) {
    NODO_PALAVRA *n;
    for (n = lista->primeira; n; n = n->proxima) {
        printf("%s\n", n->conteudo);
    }
}

int main() {
    char p[201], x = 'n';

    LISTA_PALAVRA palavras;
    palavras.primeira = NULL;
    palavras.ultima = NULL;

    while (x != 'S' && x != 's') {
        printf("\nDigite a palavra a ser inserida no jogo da forca: ");
        scanf("%200s", p);
        adicionar_palavra(&palavras, p);
        x = 'u';
        while (x != 's' && x != 'n' && x != 'S' && x != 'N') {
            printf("Deseja sair (S | N)? ");
            scanf("%c", &x);
       }
    }

    printf("Saiu\n");
    mostrar_lista(&palavras);
    limpar_lista(&palavras);
    getchar();
    return 0;
}

Browser other questions tagged

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