Michele Bottini
Michele Bottini

Reputation: 15

Filling Char* in struct field (list)

I'm having troubles passing by working on a list with "int" keys to a "char *" keys. the struct looks like :

struct nodo {
char *info;
struct nodo *prec;
struct nodo *succ;
};
typedef struct nodo nodo;

and I'm trying to use a function i used a lot to fill a struct with int or float fields (adapted obviously):

struct nodo *Crealista(void) {
    struct nodo *p, *primo, *back;
    int i, n;
    p = NULL;
    primo = NULL;
    back = NULL;
    printf("Numero di elementi: ");
    scanf("%d", &n);
    assert(n!=0);
    printf("inserisci %d numeri interi positivi: ", n);

for (i=0; i<n; i++) {
    p = malloc(sizeof(struct nodo));
    scanf("%c" /* or maybe s? i need a string for each nodo... */, p->info); 
  // i feel this is the line that needs some more work
    p->prec = back;
    p->succ = NULL;
    if (p->prec)
        p->prec->succ = p;
    back = p;
}

     primo = p;

     while (primo != NULL && primo->prec != NULL)
         primo = primo->prec;

return primo;
}

Any suggestions?

Upvotes: 2

Views: 164

Answers (1)

scopeInfinity
scopeInfinity

Reputation: 193

Just add & in scanf in the line you mentioned,

        scanf(" %c", &p->info); 

and give a space before %c in format string to " %c" so that it display text in format you want.

Upvotes: 1

Related Questions