C中全局指针的逻辑错误

时间:2015-02-01 05:56:13

标签: c pointers linked-list logic

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

我正在创建一个链接列表,其中包含一个指向列表头部的全局指针。我在主线程中创建了一个虚拟节点。我想要发生的是能够调用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);
}

2 个答案:

答案 0 :(得分:2)

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

即可初始化全局实例

答案 1 :(得分:0)

建议从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] = '\0'; 
head->next = NULL;
head->previous = NULL;

注意:在打印功能中,检查'head'的内容 在进入任何引用某个偏移量的循环之前不是NULL 从'head'开始检查,代码将尝试 取消引用内存地址0,导致 未定义的行为,可以/将导致seg故障事件