c递归函数传递char数组

时间:2012-03-28 09:19:39

标签: c recursion

我的功能如下:

void insert(btnode **ptr, char *name, unsigned int race, unsigned int class, unsigned int id, char *guild)
{
    if((*ptr) == NULL)
    {
        (*ptr) = (btnode*)malloc(sizeof(btnode));
        (*ptr)->rec = (record*)malloc(sizeof(record));
        (*ptr)->left=NULL;
        (*ptr)->right=NULL;
        strcpy((*ptr)->rec->name,name);
        (*ptr)->rec->race = race;
        (*ptr)->rec->class = class;
        (*ptr)->rec->id = id;
        strcpy((*ptr)->rec->guild, guild);
    }
    else
    {
        if((*ptr)->rec->id > id)
        {
            insert(&((*ptr)->left),name,race,class,id,guild);
        }
        else
        {
            insert(&((*ptr)->right),name,race,class,id,guild);
        }
    }
}

用于将值插入二叉树

我遇到的问题是第一个节点为null时一切正常。但是当函数必须调用它自己时,char数组不会打印它的意思。

有关如何解决此问题的任何建议吗?

编辑:添加了完整代码,未签名的整数只有字符没有问题。

struct decelerations:

#define TWOBYTEINT 16
#define FOURBYTEINT 32
#define MAXIMUMLINE 70
#define FALSE 0
#define TRUE 1

typedef struct record
{
        char name[13];
        unsigned int race : TWOBYTEINT;
        unsigned int class : TWOBYTEINT;
        unsigned int id : FOURBYTEINT;
        char guild[30];
}__attribute__((packed)) record;

typedef struct node
{
        record *  rec;
        struct node *right, *left;
}btnode;

2 个答案:

答案 0 :(得分:1)

strcpy看起来非常狡猾 - 他们看起来正在复制到(* ptr) - > rec结构中未初始化内存所指向的未分配内存。

感到惊讶的是你的代码没有崩溃。

答案 1 :(得分:0)

代码没有问题,只是删除了一些坏习惯。 (不幸的是,我保留了打包属性)

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

typedef struct record {
        char name[13];
        unsigned race : 16;
        unsigned class : 16;
        unsigned id : 32;
        char guild[30];
        }__attribute__((packed)) record;

record *record_new(char *name, unsigned int race, unsigned int class, unsigned int id, char *guild);

typedef struct node {
        record *  rec;
        struct node *right, *left;
        } btnode;

void insert(btnode **ptr, char *name, unsigned int race, unsigned int class, unsigned int id, char *guild);

void insert(btnode **ptr, char *name, unsigned int race, unsigned int class, unsigned int id, char *guild)
{
    while( *ptr ) { /* may need to check for (*ptr)->rec, too .. */
        ptr = ((*ptr)->rec->id > id)
            ? &(*ptr)->left
            : &(*ptr)->right;
        }
    (*ptr) = malloc(sizeof **ptr);
    if (!*ptr) return;
    (*ptr)->left=NULL;
    (*ptr)->right=NULL;
    /* This could cause failures elsewhere ... */
    (*ptr)->rec = record_new (name,race, class, id, guild);
}

record *record_new(char *name, unsigned int race, unsigned int class, unsigned int id, char *guild)
{
    record *rec ;
    rec = malloc(sizeof *rec);
    if (!rec) return NULL;
    strncpy(rec->name,name, sizeof rec->name);
    rec->name[sizeof rec->name-1] = 0;
    rec->race = race;
    rec->class = class;
    rec->id = id;
    strncpy(rec->guild,guild, sizeof rec->guild);
    rec->guild[sizeof rec->guild-1] = 0;
    return rec;
}

BTW:我删除了递归,因为它不需要。

相关问题