关于如何实现在insert函数的参数中找到的char指针,我有点困惑。这个函数没有char * c参数,但我意识到我需要包含它,因为结构有一个字符串名称字段(最多25个字符)。
我希望我解释得很好......我想知道它在哪里我将包含在此函数中添加名称的代码:
struct data_node * insert(struct data_node **p_first, int elem, char *c) {
struct data_node *new_node, *prev, *current;
current=*p_first;
while (current != NULL && elem > current->data) {
prev=current;
current=current->next;
} /* end while */
/* current now points to position *before* which we need to insert */
new_node = (struct data_node *) malloc(sizeof(struct data_node));
new_node->data=elem;
new_node->next=current;
if ( current == *p_first ) /* insert before 1st element */
*p_first=new_node;
else /* now insert before current */
prev->next=new_node;
/* end if current == *p_first */
return new_node;
}; /* end insert */
struct data_node:
#define STRINGMAX 25
struct data_node {
char name [STRINGMAX];
int data;
struct data_node *next;
};
答案 0 :(得分:1)
在初始化其余部分时初始化name
。
new_node = malloc(sizeof(struct data_node));
new_node->data=elem;
strncpy(new_node->name, name, STRINGMAX);