我正在处理链表,与之前尝试解决的略有不同,这导致了一些问题。我熟悉使用简单的整数参数处理链表的过程,但是我试图处理一个字符数组,在这种情况下无法确切地知道如何添加到列表中:

struct process{
    pid_t pid;
    char userArgument[1024];
    struct process* next;
};

class processList{
    private:
    process *head, *tail;
    public:
    processList(){
        head = NULL;
        tail = NULL;
    }

    void add(int pid, char argument[]){
        process *tmp = new process;
        tmp->pid = pid;
        tmp->userArgument = argument; //PROBLEM. I want this to take a character array passed to add() and use it as the userArgument for this new process
        tmp->next = NULL;

        if(head == NULL){
            head = tmp;
            tail = tmp;
        }
        else{
            tail->next = tmp;
            tail = tail->next;
        }

    }
};


add函数的预期行为是创建一个int类型的pid和一个char []类型的新userArgument的新进程。但是,在我标记为问题的行中,它引发了一个错误,并且我尝试了其他版本,但均未成功(将add()传递给字符串,而使用c_str()等)。我一直在努力使此工作正常进行,并希望能提供任何帮助。

最佳答案

   strcpy(tmp->userArgument, argument);


应该可以。

关于c++ - C++添加到包含Char []作为参数的链接列表,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48511035/

10-11 19:30