c - 无法在结构中分配字符串

标签 c string struct malloc

我的结构看起来像这样:

struct tree{
    char *name;
    int num_subdirs;
    struct tree **subdirs;
}

我收到一个缓冲区,其中包含在缓冲区中序列化的整个树。我试图在这个函数中反序列化它:

struct tree *t;
//buffer is filled, received from somewhere else.
int res = deserialize(t, buf); //call function deserialize

//deserialize function
            //buf = {../,2}{sd,0}{|}{sr,1}{sk,0}{|}{|}
   │406     int dfsDeserialize(struct tree *dt, void *buf, int *q){                                                                                                                       │
   │407         char name[MAXPATHLEN];                                                                                                                                                           │
   │408         char delim[3];                                                                                                                                                                   │
   │409         int len, numsubs, i;                                                                                                                                                             │
                                                                                                                                                        │
   │411         sscanf(buf+(*q),"%3s",delim);                                                                                                                                                    │
   │412         if(!strcmp(delim,"{|}")){                                                                                                                                                        │
   │413             (*q)+=3;                                                                                                                                                                     │
   │414             return 1;                                                                                                                                                                    │
   │415         }                                                                                                                                                                                │
   │416         sscanf((buf + (*q)), "{%[^,],%d}%n", name, &numsubs, &len);                                                                                                                      │                                                                                                                                          │
  >│419         int slen = strlen(name);                                                                                                                                                         │
   │420         dt->name = calloc(slen + 1, 1);                                                                                                                                                  │
   │421         dt->subdirs = malloc(numsubs*sizeof(struct tree *));                                                                                                                      │
   │422         strcpy(dt->name, name);                                                                                                                                                          │
   │423         dt->num_subdirs = numsubs;                                                                                                                                                       │
   │424         (*q)+=len;                                                                                                                                                                       │
   │425         for(i = 0; i< numsubs; i++){                                                                                                                                                     │
   │426             dt->subdirs[i] = malloc(sizeof(struct tree));                                                                                                                         │
   │427             dfsDeserialize(dt->subdirs[i], buf, q);                                                                                                                                      │
   │428         }                                                                                                                                                                                │
   │429         return 0;                                                                                                                                                                        │
   │430     }   

                                                                                                                                                                             │

我已经尝试了几种不同的方法来为字符串分配内存,但每次都失败了!我不知道为什么 t->name 总是 0x0。请帮忙。

最佳答案

我认为罪魁祸首是这个

t = malloc(sizeof(sizeof tree));

你可能是说

t = malloc(sizeof(struct tree));

您还可以使用更方便的 strdup 在堆上复制一个字符串

t->name = strdup(name);

关于c - 无法在结构中分配字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35238872/

相关文章:

复制文件到 windir c

c - GDB,中断条件,检查空指针

c - 我正在寻找几何代码,但我不知道 p1[X] 代码

c - 使用 stderr 的段错误

c - 从字符串 C 中获取 int 值

java - 如何获取包含在指定字符串中的特定字符串?

java - 根据某些值在java中分割字符串

c# - 从 string[] 生成查询字符串

c - 指针有问题吗?

c++ - 结构中的数组 C++