Saving names

2

I want to allocate a string inside any structure so I can fetch these strings afterwards. I need to enter a condition to end the insertion of names. I'm thinking of a structure that is running until the end of the reading, I think it would be a vector of pointers. These pointers would be created the moment the string is inserted. Any help is welcome, thanks.

    
asked by anonymous 11.02.2016 / 03:51

1 answer

0

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

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 enter as many names as you want in C.

    
15.02.2016 / 07:56