传递引用导致分段错误

时间:2015-09-17 00:14:05

标签: c segmentation-fault

我正在尝试创建循环链接列表。当我尝试通过将头指针作为参考传递来添加元素时,它会引发分段错误。我注意到,一旦调用Add元素函数,头指针的值就会改变。 代码段:

struct Node {
    int data;
    struct Node *next;
};

int c = 1;
typedef struct Node node;
//node *head = NULL, *temp, *temp2, *z;
node *InitQueue(node *);
node *AddQueue(node *, int);
void DelQueue();
void display(node *);

void main()
{
    int ch;
    node *head = NULL;

    do {
        printf("1.Creation Q\n");
        printf("2.Insert element to Q\n");
        printf("3.Delete element\n");
        printf("4.Display Q\n");
        printf("5.Exit\n");
        printf("Enter your choice:\n");

        scanf("%d", &ch);
        switch (ch) {
        case 1:
            head = InitQueue(&head);
            printf("%d %p\n", head->data, head->next);
            break;

        case 2:
            printf("%d %p\n", head->data, head);
            int item;
            printf("Enter item\n");
            scanf("%d", &item);
            head = AddQueue(&head, item);
            break;

        case 3:
            DelQueue();
            break;

        case 4:
            display(&head);
            break;

        case 5:
            exit(0);
        }

    } while (ch != 5);

}

node *InitQueue(node * head)
{
    node *temp;
    temp = (node *) malloc(sizeof(node));
    printf("%p \n", temp);
    temp->next = temp;
    head = temp;
    printf("%p \n", head->next);
    return head;
}

node *AddQueue(node * head, int item)
{
    //InitQueue(&head);
    printf("%d %p\n", head->data, head);
    node *temp, *temp2;
    temp = head;
    temp2 = (node *) malloc(sizeof(node));
    //printf("Enter the data: \n");
    //scanf("%d", &temp2->data);
    temp2->data = item;

    while (temp->next != head) {
        temp = temp->next;
    }
    temp->next = temp2;
    temp->next = head;
    head = temp2;
    return head;
}

2 个答案:

答案 0 :(得分:3)

您的问题是您将变量filp_open的实际内存地址传递给headInitQueue函数(),以便您可以在函数内修改它),但它们被声明为:

AddQueue

您的功能期待node *InitQueue(node *); node *AddQueue(node *, int); 并且您通过了node *

执行此操作时:

node **
    1. 类型错误。您的编译器抱怨它而您忽略了
    1. head = InitQueue(&head); ... AddQueue(&head, item); 只有在你回来的时候才有效。
    1. InitQueue不起作用,因为它期待的是AddQueue,而不是pointer to node

你应该这样做:

pointer to pointer to node

现在你以与调用相同的方式调用它,但不需要返回任何内容。

void InitQueue(node **head)
{
    (*head) = malloc(sizeof(node));
    if (!(*head)) { /* error check */ }
    (*head)->next = (*head);
}

您可以通过相同的方式修复InitQueue(&head);

可能有兴趣

答案 1 :(得分:0)

关于此功能,作为如何进行操作的示例:

//node *InitQueue(node * head) // passing 'head' here is useless
node *InitQueue( int data ) // use 'data' to fill in field on first node
{
    node *temp = NULL;

    // always check the returned value from malloc 
    // to assure the operation was successful
    // if not successful, handle the error
    if( NULL == (temp = malloc(sizeof(node)) ) )
    { // then malloc failed
        perror( "malloc for initial node failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, malloc successful

    //printf("%p \n", temp);  // debug statement

    temp->data = data;
    temp->next = NULL;    // always set the 'link' to NULL so end of list can be found

    //head = temp;    // head is a copy on the stack, so has no effect on callers 'head'

    //printf("%p \n", head->next); // debug statement

    //return head;  // this is not set to point to the malloc'd node
    return temp;
}

使用它来初始化带有第一个条目的链表时存在一些问题。

推荐:

addData()