C 中的全局指针出现逻辑错误



我发现了如何解决我的问题,但我不知道它是如何或为什么工作的。我非常感谢有人看看这个:

我正在创建一个带有全局指针指向列表头部的链表。我在主线程中创建了一个虚拟节点。我想要发生的是能够调用 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] = ''; 
    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] = ''; 
    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] = ''; 
    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("nnBreaking....");
            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:%dn", count);
}

没有状态传递到printList函数中,因此head变量引用全局实例。它不适合您的原因是您的main函数未使用全局实例。当您在 main 函数中键入 Person *head = NULL; 时,它会声明一个局部变量(即,您没有修改全局实例)。相反,您应该通过简单地键入 head = NULL; 来初始化全局实例。

建议从main()中(完全)删除以下行因为他们 1) 屏蔽全局变量"head" 2) 将第一个条目放入链表 这就是为什么计数等 当链表中没有专门插入任何内容时

Person *head = NULL; 
printf("nmain head:%p n", head);
head = (Person *)malloc(sizeof(Person));
printf("nmain head:%p n", head);
head->name[0] = ''; 
head->next = NULL;
head->previous = NULL;

注意:在打印功能中,检查"头"的内容而不是 NULL 在进入任何引用某些偏移量的循环之前从"head"和没有该检查一样,代码将尝试取消引用内存地址 0,导致未定义的行为,可能/将导致 SEG 故障事件

相关内容

  • 没有找到相关文章

最新更新