简体   繁体   English

通过char数组的c递归函数

[英]c recursive function passing through char array

I have function along the lines of: 我具有以下功能:

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

It is use to insert values into a binary tree 用于将值插入二叉树

The issue I'm having is when the first node is null everything works fine. 我遇到的问题是,当第一个节点为null时,一切正常。 But when the function has to call its self the char array doesn't print what its meant to. 但是,当函数必须调用其自身时,char数组不会显示其含义。

Any suggestions how to solve this issue? 有什么建议如何解决这个问题?

EDIT: full code added, there is no problems with unsnigned ints only chars. 编辑:完整的代码添加,不带警惕的整数只有字符没有问题。

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;

The strcpy look extremely dodgy - they look they are copying to unallocated memory pointed to by the uninitialised memory in the (*ptr)->rec structure. strcpy看起来非常狡猾-他们看起来将它们复制到(* ptr)-> rec结构中未初始化内存所指向的未分配内存中。

Surprised your code doesn't crash. 惊讶您的代码不会崩溃。

Nothing wrong with the code, just remove some bad habits. 代码没有错,只需消除一些不良习惯即可。 (I kept the packed attribute, unfortunately) (不幸的是,我保留了packed属性)

#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: I removed the recursion, since it is not needed. 顺便说一句:我删除了递归,因为它是不必要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM