如何将引用传递给以引用作为参数的函数

时间:2016-07-13 02:30:35

标签: c pointers

我无法弄清楚如何使我的程序工作。该程序要求用户输入一个数字并打印出来。我使用链表来存储我的值。但不能让它工作,需要帮​​助 。我使用structure.h文件来定义我的结构

#ifndef STRUCTURE_H_
#define STRUCTURE_H_

struct intNode
{
    int value;
    struct intNode *next;
};

#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "structure.h"
#define commandLength 6
#define initSize 100

void insert(struct intNode *head, struct intNode *current);
void print(struct intNode *head);

int main()
{
    char command[commandLength];
    struct intNode *head = NULL;
    struct intNode *current = NULL;

    printf("Enter a command from the following list of commands:\n\n"
            "insert - to add a number to the list;\n"
            "print  - to print out the list;\n"
            "exit   - to terminate the program;\n\n"
            "Enter your command:\n");

    while (strcmp(command, "exit") != 0)
    {
        scanf( "%s",command);

        if(strcmp(command,"insert") == 0)
        {insert(head, current);}
        else if(strcmp(command, "print") == 0)
        {print(head);}
        else if(strcmp(command, "exit") == 0)
        {printf("\nThe program has been terminated.");}
        else
        {printf("Error: unknown request '%s'\n", command);}
    }//end of while
}//end of main

void insert(struct intNode *head, struct intNode *current)
{
    struct intNode *node;
    node = (struct intNode *) malloc(sizeof(struct intNode));
    printf("enter a number:");
    scanf("%d",&node->value);
    if(head == NULL)
    {
        head = node;
    }
    else
    {
        if(head->next == NULL)
        {
            head->next = node;
            current = node;
        }else{
            printf("%d ",current->value);
            current->next = node;
            current = node;
        }
    }
}//end of insert

void print(struct intNode *head)
{
    struct intNode *temp;
    temp = head;

    while(temp != NULL)
    {
        printf("%d ",temp->value);
        temp = temp->next;
    }
}//end of print

2 个答案:

答案 0 :(得分:0)

insert(head, current);函数中,您传递参数但它们也是输出参数。因此,您需要传递它们的地址,然后更改它们的值,这些值将反映在main()中。因此,将该调用更改为

insert(&head, &current)

并将您的功能更新为

void insert(struct intNode **head, struct intNode **current)
{
    struct intNode *node;
    node = (struct intNode *) malloc(sizeof(struct intNode));
    printf("enter a number:");
    scanf("%d",&node->value);
    //--v note * here and other places
    if(*head == NULL)
    {
        *head = node;
    }
    else
    {
        if((*head)->next == NULL)
        {
            (*head)->next = node;
            *current = node;
        }else{
            printf("%d ",(*current)->value);
            (*current)->next = node;
            (*current) = node;
        }
    }
}//end of insert

答案 1 :(得分:0)

我已经创建了两个版本的代码。一个注释了[大部分]错误和清理工作版本[请原谅无偿风格清理]。

当您在插入中设置head时,它并未在main中设置此项,仅在insert中设置。我使用&#34; dummy&#34;解决了这个问题。节点作为列表标题方法。

以下是带注释的版本:

#ifndef STRUCTURE_H_
#define STRUCTURE_H_

struct intNode {
    int value;
    struct intNode *next;
};

#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "structure.h"
#define commandLength 6
#define initSize 100

void insert(struct intNode *head, struct intNode *current);
void print(struct intNode *head);

int
main()
{
    char command[commandLength];
    struct intNode *head = NULL;
    struct intNode *current = NULL;

    printf("Enter a command from the following list of commands:\n\n" "insert - to add a number to the list;\n" "print  - to print out the list;\n" "exit   - to terminate the program;\n\n" "Enter your command:\n");

    // NOTE/BUG: on the first time through the loop, command is invalid, so do
    // this:
#if 1
    command[0] = 0;
#endif
    while (strcmp(command, "exit") != 0) {
        scanf("%s", command);

        // NOTE/BUG: passing current serves no purpose
        if (strcmp(command, "insert") == 0) {
            insert(head, current);
        }
        else if (strcmp(command, "print") == 0) {
            print(head);
        }
        else if (strcmp(command, "exit") == 0) {
            printf("\nThe program has been terminated.");
        }
        else {
            printf("Error: unknown request '%s'\n", command);
        }
    }                                   // end of while
}                                       // end of main

void
insert(struct intNode *head, struct intNode *current)
{
    struct intNode *node;

    node = (struct intNode *) malloc(sizeof(struct intNode));
    printf("enter a number:");
    scanf("%d", &node->value);

    // NOTE/BUG: when head is set here, it only modifies it within _this_
    // function and does _not_ update it in main
    if (head == NULL) {
        head = node;
    }

    // NOTE: this is closer to the insertion you want to do
    else {
        if (head->next == NULL) {
            head->next = node;
            current = node;
        }
        else {
            printf("%d ", current->value);
            current->next = node;
            current = node;
        }
    }
}                                       // end of insert

void
print(struct intNode *head)
{
    struct intNode *temp;

    temp = head;

    while (temp != NULL) {
        printf("%d ", temp->value);
        temp = temp->next;
    }
}                                       // end of print

这是清理后的工作版本。我将head重命名为list,以便更好地描述其新角色。

#ifndef STRUCTURE_H_
#define STRUCTURE_H_

struct intNode {
    int value;
    struct intNode *next;
};

#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//#include "structure.h"

#define commandLength 6
#define initSize 100

void insert(struct intNode *list);
void print(struct intNode *list);

int
main()
{
    char command[commandLength];
    struct intNode list;

    list.next = NULL;

    while (1) {
        printf("Enter a command from the following list of commands:\n\n" "insert - to add a number to the list;\n" "print  - to print out the list;\n" "exit   - to terminate the program;\n\n" "Enter your command:\n");

        scanf("%s", command);

        if (strcmp(command, "exit") == 0) {
            printf("\nThe program has been terminated.\n");
            break;
        }

        if (strcmp(command, "insert") == 0) {
            insert(&list);
            continue;
        }

        if (strcmp(command, "print") == 0) {
            print(&list);
            continue;
        }

        printf("Error: unknown request '%s'\n", command);
    }
}

void
insert(struct intNode *list)
{
    struct intNode *current;
    struct intNode *prev;
    struct intNode *node;

    node = malloc(sizeof(struct intNode));
    node->next = NULL;

    printf("enter a number:");
    scanf("%d", &node->value);

    // find the end of the list
    prev = NULL;
    for (current = list->next;  current != NULL;  current = current->next)
        prev = current;

    if (prev != NULL)
        prev->next = node;
    else
        list->next = node;
}

void
print(struct intNode *list)
{
    struct intNode *temp;

    for (temp = list->next;  temp != NULL;  temp = temp->next)
        printf("%d ", temp->value);
    printf("\n");
}