将参数传递到动态数组的链表中会导致覆盖值

时间:2013-02-11 00:38:54

标签: c linked-list

我将参数传递给链表时遇到问题。 我等待用户输入,然后解析此用户输入并将解析后的令牌放入动态数组(char ** cmd)

之后我需要将某些用户输入写入链表(例如我需要将cmd [1]传递给链表) 并且此过程在while循环中重复,直到用户输入“exit”(因此将要求用户输入另一个输入,然后我需要再次解析它等等)

但是在第一个循环之后(用户输入输入,我解析它然后发送cmd [1]和cmd [2]链接列表)我再次解析字符串然后再次发送cmd [1]和cmd [ 2]到链表。然而,我的链接列表会将所有先前的值覆盖到新的值,然后使用新值添加新节点,以便我的所有节点现在具有相同的值。 我刚刚开始学习c月份,所以我可能有一个指针或者它的链表有问题,即使我用c ++写了这个链表,现在我把它转换成我转换后可能会有一些错误。我还测试了我的链表,只是传递了不是来自动态数组的常规参数,而且似乎工作正常。

#include <stdio.h>
#include <string.h>
#include <stdbool.h> 
#include <stdlib.h>

//struct for environmental variables and thier values
struct ListNode
{
    //variable and value in the node
    char *var,*value;
    struct ListNode* next;//point to the next node
};
struct ListNode* head;//pointer to the head
// head = malloc(sizeof(struct ListNode));

void printList();
void appendNode(char *v, char *val);

int main(int argc, const char * argv[])
{
    char user_input[20]={0};

    int i=0;
    char c;

    int count=0;//keep count of words the user entered

    char ** cmd  = NULL;//create pointer and set it to NULL
    int size = 0;


    while (strcmp(user_input, "exit") != 0)//check if exit was typed into cmd line, if so then exit
    {

        //input three words separated with space
        scanf("%50[^\n]", user_input);//scan user input 


        size=0;
        count=0;
        const char delimiters[] = " !-";//create an array of delimiters to use to separate string

        char *ptr = strtok (user_input, delimiters);

        cmd = malloc(sizeof(char));//allocate memory for pointer cmd

        while (ptr)//split string into tokens to " !-"
        {
            cmd = realloc (cmd, sizeof(char*)*(++size));//reallocate more memory for an array

            if (cmd == NULL)
                exit (-1); // memory allocation failed

            cmd[size-1] = ptr;

            ptr = strtok (NULL, delimiters);

            count++;//count words in the input
        }


        cmd = realloc (cmd, sizeof(char*)*(size+1));// realloc one extra element for the last NULL
        cmd[size] = 0;

        for (i = 0; i < (size+1); ++i)
            printf ("cmd[%d] = %s\n", i, cmd[i]);


         //free(cmd);//free memory


        while((c = getchar()) != '\n' && c != EOF); //flush buffer


        if (count==3 )//set environmental variable

            {

                appendNode(cmd[1], cmd[2]);
            }
        printList();

    }
     free(cmd);
    return 0;
}


void appendNode(char *v, char *val)
{
    struct ListNode* newNode;//to point to new node
    struct ListNode* nodePtr = NULL;//to move through the list

    //allocate new node and store int there
    newNode = malloc (sizeof( struct ListNode));
    newNode->var=v;//put value of v into new variable
    newNode->value=val;//put value of val into new value
    newNode->next = NULL;
    //if there are no nodes in the list make new node th efirst node
    if(!head)
    {
        head = newNode;

    }
    else
    {
        //initialize nodePtr to head
        nodePtr = head;
        //find the last node in the list
        while (nodePtr->next)
        {
            nodePtr = nodePtr->next;
        }
        //insert new node as the last node
        nodePtr->next = newNode;
    }

}
void printList()
{
    struct ListNode* nodePtr = head; //position nodePtr at the head
    //while noePtr points to node, traverse the list
    while(nodePtr)
    {
        //display value
        printf("%s\n",nodePtr->var);
        //move to next node
        nodePtr = nodePtr->next;
    }

}

1 个答案:

答案 0 :(得分:1)

问题是由于发送了本地指针cmd,每次为主例程中的新字符串重新分配。

这可以通过修改appendnode API来解决

void appendNode(char *v, char *val)
{
    struct ListNode* newNode;//to point to new node
    struct ListNode* nodePtr = NULL;//to move through the list

    //allocate new node and store int there
    newNode = malloc (sizeof( struct ListNode));
    newNode->var = malloc(sizeof(v));
    newNode->value = malloc(sizeof(val));   
    strcpy(newNode->var, v);
    strcpy(newNode->value, val);    
    newNode->next = NULL;
    //if there are no nodes in the list make new node th efirst node
    if(!head)
    {
        head = newNode;

    }
    else
    {
        //initialize nodePtr to head
        nodePtr = head;
        //find the last node in the list
        while (nodePtr->next)
        {
            nodePtr = nodePtr->next;
        }
        //insert new node as the last node
        nodePtr->next = newNode;
    }

}