在结构字段(列表)中填充 Char*

Filling Char* in struct field (list)

我在处理使用 "int" 键到 "char *" 键的列表时遇到了麻烦。 struct 看起来像:

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

我正在尝试使用我经常使用的函数来填充 struct 和 int 或 float 字段(明显改编):

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;
}

有什么建议吗?

只需在您提到的行中的 scanf 中添加 &

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

并在格式字符串 %c 之前给 space 到 " %c" 以便它以您想要的格式显示文本。