【发布时间】:2014-05-08 17:10:33
【问题描述】:
我正在尝试实现链表的头插入功能,并希望通过引用传递将 void* 指针返回到新插入的节点。不幸的是,我无法更改参数。这是我的相关代码:
typedef struct List_t_def{
spinlock_t * lock_counter;
unsigned int key;
struct List_t_def *next;
}list_t;
typedef volatile unsigned int spinlock_t;//basic lock
void List_Insert(list_t *list, void *element, unsigned int key) {
list_t * list_new = (list_t *)malloc(sizeof(list_t));
spinlock_t * lock_temp = (spinlock_t*)malloc(sizeof(spinlock_t));
list_new->lock_counter = lock_temp;
spinlock_acquire(list->lock_counter);
list_new->key = key; //inserting the new created node as the first one (head of the linked list)
list_new->next = list->next;
list_new->lock_counter = list->lock_counter;
list->next = list_new;
element = (void*)list_new;
spinlock_release(list->lock_counter);
return;
}
我正在尝试将element 设置为新插入节点的开头,但是当它返回时,element 不会更改其先前的值。任何建议或帮助表示赞赏,谢谢!
【问题讨论】:
-
您需要将元素作为 void 传入**
-
我无法更改函数的参数。
-
好吧,那我觉得你倒霉了。
-
可能有助于向人们展示调用该函数的代码。
-
list->next = list_new 基本上分配给“list”输入参数。调用者可以利用这个作为调用函数提供的“列表”内存。这对您的程序有意义吗?
标签: c pointers linked-list pass-by-reference