Keeping names

Asked

Viewed 49 times

2

I want to allocate a string within any structure that I can search for these strings after. I need to enter a condition to end the name insertion. I’m thinking of a structure that runs until the end of the reading, I think it would be a pointer vector. These pointers would be created at the time the string is inserted. Any help is welcome, thank you.

  • 1

    Welcome, Johann. Use tags to identify the programming language you are using.

  • Language C....

  • Basically you have 2 hypotheses: the pointer vector or a "Linked list".

  • And how would these lists Linked?

1 answer

1

You can use a list structure to add as many names as you want.

typedef struct __list__{
    const char *name;
    struct __list__ *next;
}list; // Estrutura de linked list

void list_insert(list **l, const char *name){
    // Cria uma nova posição na lista
    list *_new = (list*) malloc(sizeof(list));
    _new->name = name;
    _new->next = NULL;

    if(!*l){ // Se a lista estiver vazia, Insere o primeiro item
        *l = _new;
        return;
    }

    list *buff = *l;
    while(buff->next)
        buff = buff->next; // Anda até o final da lista

    buff->next = _new; // inseri um novo item no final da lista
}

with this structure, you will be able to insert as many names as you want in C.

Browser other questions tagged

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