我发现了如何解决我的问题,但我不知道它是如何或为什么起作用的。我将非常感谢有人看看这个:
我正在创建一个链接列表,其中包含指向列表头部的全局指针。我在主线程中创建了一个虚拟节点。我想要发生的是能够调用 printList() 并且如果除了虚拟节点之外没有其他节点然后打印“Person:0”(基本上说列表是空的)。
[编辑 -> 这是我的简洁问题:为什么 printList() 识别 main() 中的 Person *head = NULL 而不是全局指针,当它使用它来设置当前指针等于 head 时?]
使用这段代码我得到以下输出
int main(){
setvbuf(stdout, NULL, _IONBF, 0);
//Person *head = NULL;
printf("\nmain head:%p \n", head);
head = (Person *)malloc(sizeof(Person));
printf("\nmain head:%p \n", head);
head->name[0] = '\0';
head->next = NULL;
head->previous = NULL;
输出:
main head:0000000000000000
main head:00000000003F1390
Enter add, insert or delete for Person functions: print
printList head:00000000003F1390 Person:1 Total People:1
Enter add, insert or delete for Person functions:
在 main() 中声明 Person*head 并将其初始化为 NULL,我得到了想要的结果。 为什么会这样?为什么我无法初始化全局指针并获得相同的预期结果?
int main(){
setvbuf(stdout, NULL, _IONBF, 0);
Person *head = NULL;
printf("\nmain head:%p \n", head);
head = (Person *)malloc(sizeof(Person));
printf("\nmain head:%p \n", head);
head->name[0] = '\0';
head->next = NULL;
head->previous = NULL;
以下输出:
main head:0000000000000000
main head:00000000005E1390
Enter add, insert or delete for Person functions: print
printList head:0000000000000000 Total People:0
Enter add, insert or delete for Person functions:
这里是整个程序供引用:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
typedef struct S_PersonInLine{
char name[16];
struct S_PersonInLine *next;
struct S_PersonInLine *previous;
}Person;
//pointer to head of the list
//This isn't a global head pointer(wrong). It doesn't go into main (wrong). False it does go into main but it doesn't give the intended result from the printList
Person *head = NULL; //this allows the functions to access the head pointer
//prototypes
Person *makePerson();
void *addPerson();
void *insert();
void *delete();
void printList();
void cleanUp();
int main(){
setvbuf(stdout, NULL, _IONBF, 0); //Figure out what this thing does again and why its necessary
Person *head = NULL;
printf("\nmain head:%p \n", head);
head = (Person *)malloc(sizeof(Person));
printf("\nmain head:%p \n", head);
head->name[0] = '\0';
head->next = NULL;
head->previous = NULL;
char input[16];
char command[16];
printf("Enter add, insert or delete for Person functions: ");
while( fgets(input , 15 , stdin) ){
sscanf(input, "%s", command);
if ( strcmp(command, "quit") == 0 ){
printf("\n\nBreaking....");
break;
} else if ( strcmp(command, "print") == 0 ){
printList();
}
printf("Enter add, insert or delete for Person functions: ");
}
return 0;
}
void printList(){
Person *current = head;
printf("\nprintList head:%p ", head);
int count = 0;
while(current != NULL){
count++;
printf("Person:%d %s", count, current->name);
current = current->next;
}
printf("Total People:%d\n", count);
}
最佳答案
没有状态被传递到 printList
函数,因此那里的变量 head
指的是全局实例。它不适合你的原因是因为你的 main
函数没有使用全局实例。当您在 main
函数中键入 Person *head = NULL;
时,它会声明一个局部变量(即,您没有修改全局实例)。相反,您应该通过简单地键入 head = NULL;
来初始化全局实例。
关于c - C中全局指针的逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28259953/