【发布时间】:2016-03-04 05:35:27
【问题描述】:
struct node
{
char *IPArray[100];
struct node *ptr;
};
typedef struct node NODE;
NODE *head, *first, *temp = 0;
first = 0;
int numofIP;
这是我的结构,用于在链表中的每个节点处包含字符串。 numofIP 是我的链表中的最大字符串或节点数。
for(int i=0; i<numofIP; i++)
{
head = (NODE *)malloc(sizeof(NODE));
printf("enter the IP addresses:\n");
scanf("%s",&head->IPArray[i]);
if(first != 0)
{
temp->ptr = head;
temp = head;
}
else
{
first = temp = head;
}
}
temp->ptr = 0;
temp = first;
这就是我接受输入并将其存储在每个节点中的方式。
while(temp != NULL)
{
printf("%d=> ",temp->IPArray);
temp = temp->ptr;
}
这就是我打印链接列表的方式。
但问题是我在输出中得到地址。我无法弄清楚。如何在链表的每个节点中存储一个字符串?
【问题讨论】:
-
char *IPArray[100];→char IPArray[100];,scanf("%s",&head->IPArray[i]);→scanf("%s", head->IPArray[i]);,printf("%d=> ",temp->IPArray);→printf("%s=> ",temp->IPArray); -
你的字符数组“char *IPArray[100]”其实是一个字符指针数组,里面存放的是字符地址。
标签: c linked-list