[英]Typecasting char * to struct * in C
char *constant
是指向我的鏈表頭部的指針。 我應該在我的函數中使用全局 char 指針“常量”,而我正在做的是 -
typedef struct Node
{
int attribute;
struct Node next;
} Node;
void init(int data)
{
Node *constant = (Node *)malloc(sizeof(Node));
constant->attribute = data;
Node *next = (Node *)malloc(sizeof(Node));
next->attribute = data*2;
constant->next = next;
next->next = NULL;
printList();
}
void printList()
{
Node *temp = (Node *)constant;
while(temp != NULL)
{
printf("%d\n", temp->attribute);
temp = temp->next;
}
}
當我在 init function 中調用 printList 時,它不會打印任何內容,但是當我將 printList 代碼復制到 init 時,它會執行應有的操作。 我在其他函數中類似地使用“常量”,它給了我一個分段錯誤。 我該如何解決?
如果您的應用程序中已經有一個 char *constant,那么您肯定不想聲明一個具有相同名稱的 Node*。
從 init() 的第一行中刪除 Node* 以提供constant = malloc(sizeof(Node));
這樣您就可以實際使用全局變量,而不是本地變量。 您還需要將它與((Node*)constant)->next = next;
一起使用的任何地方投射。 例如
在您的init()
function 中,您有點困惑如何在列表中添加開始節點。 你有你的全局指針constant *head;
添加節點時,聲明並分配一個新的臨時節點,並初始化成員設置attribute = data;
next = NULL;
分配第一個節點后,將節點地址分配給head
。
不需要init()
function,您真正需要的是add()
或push()
function,它只是將數據作為參數傳遞,然后為節點分配,如上所述初始化,然后分配給next
指針列表中的最后一個節點。 (您也可以執行所謂的forward-chaining ,每次都添加一個新的第一個節點,但列表中的節點最終以與輸入方式相反的順序結束。
您可以使用兩個全局指針, head
和tail
,其中tail
將始終指向列表中的最后一個節點。 這樣您就不必迭代插入到列表的末尾,只需在tail->next = node;
處添加新節點即可。 然后更新tail,使其指向新的結束節點,例如tail = node;
假設您的struct constant
類似於:
typedef struct constant {
int attribute;
struct constant *next;
} constant;
您可以編寫add()
function 來處理將所有節點按順序添加到列表中:
constant *head, *tail;
...
constant *add (int data)
{
constant *node = malloc (sizeof *node); /* allocate */
if (!node) { /* validate EVERY allocation */
perror ("malloc-node");
return NULL;
}
node->attribute = data; /* assign data to attribute */
node->next = NULL; /* initialize next NULL */
if (!head) /* if 1st node */
head = tail = node; /* assign node to both head, tail */
else { /* otherwise */
tail->next = node; /* set tail->next = node */
tail = node; /* update tail to point to last node */
}
return node; /* return pointer to added node to indicate success */
}
您的print()
和free_list()
函數都可以簡單地遍歷每個節點,要么輸出節點屬性,要么釋放節點。 free_list()
中唯一需要注意的是,您必須在刪除當前節點之前遞增到下一個節點。 兩個函數如下圖所示:
總而言之,你可以這樣做:
#include <stdio.h>
#include <stdlib.h>
typedef struct constant {
int attribute;
struct constant *next;
} constant;
constant *head, *tail;
constant *add (int data)
{
constant *node = malloc (sizeof *node); /* allocate */
if (!node) { /* validate EVERY allocation */
perror ("malloc-node");
return NULL;
}
node->attribute = data; /* assign data to attribute */
node->next = NULL; /* initialize next NULL */
if (!head) /* if 1st node */
head = tail = node; /* assign node to both head, tail */
else { /* otherwise */
tail->next = node; /* set tail->next = node */
tail = node; /* update tail to point to last node */
}
return node; /* return pointer to added node to indicate success */
}
void prn_list (void)
{
/* loop over each node using iter and output attribute for each node */
for (constant *iter = head; iter; iter = iter->next)
printf (" %d", iter->attribute);
putchar ('\n');
}
void free_list (void)
{
/* loop over each node, but increment to next in list before deleting current */
for (constant *iter = head; iter;) {
constant *victim = iter;
iter = iter->next;
free (victim);
}
}
int main (void) {
for (int i = 0; i < 10; i++)
add (i + 1);
prn_list();
free_list();
}
示例使用/輸出
$ ./bin/ll_global_head
1 2 3 4 5 6 7 8 9 10
如果您還有其他問題,請仔細查看並告訴我。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.