在C中的节点之间交换字符串

在C中的节点之间交换字符串,c,string,linked-list,nodes,swap,C,String,Linked List,Nodes,Swap,我试图在链接列表中的节点之间交换数据字段,在交换字符数组时遇到问题。这只是该程序的一个示例 struct node { int count; char word[50]; struct node *next; }; void swap_nodes( struct node *first, struct node *second ) { int temp_count; char *temp_word; temp_count = first->

我试图在链接列表中的节点之间交换数据字段,在交换字符数组时遇到问题。这只是该程序的一个示例

struct node {
    int count;
    char word[50];
    struct node *next;
};

void swap_nodes( struct node *first, struct node *second ) {
    int temp_count;
    char *temp_word;

    temp_count = first->count;
    temp_word = first->word;
    first->count = second->count;
    first->word = second->word;
    second->count = temp_count;
    second->word = temp_word;
}

让我知道我做错了什么,我对用c写很不熟悉。

当您将字符数组分配给指针时,您不会复制该数组:

char *temp_word;
temp_word = first->word;
temp_word
指向数组的初始元素,因此分配给数组也会更改指针指向的数据

您可以通过声明一个包含50个字符的数组,并使用
strcpy
memcpy
进行复制来解决此问题:

char temp_word[50];
memcpy(temp_word, first->word, sizeof(temp_word));
memcpy(first->word, second->word, sizeof(temp_word));
memcpy(second->word, temp_word, sizeof(temp_word));

将字符数组指定给指针时,不会复制该数组:

char *temp_word;
temp_word = first->word;
temp_word
指向数组的初始元素,因此分配给数组也会更改指针指向的数据

您可以通过声明一个包含50个字符的数组,并使用
strcpy
memcpy
进行复制来解决此问题:

char temp_word[50];
memcpy(temp_word, first->word, sizeof(temp_word));
memcpy(first->word, second->word, sizeof(temp_word));
memcpy(second->word, temp_word, sizeof(temp_word));

word[50]
struct节点的一部分,它位于
struct节点的内部,而您所做的只是移动一个指针
*temp\u word
指向
*第一个
,然后指向
*第二个
,而
word[50]
的内容并没有发生深刻的变化。您可以使用
memcpy
来更改内容。

单词[50]
struct节点的一部分,它位于
struct节点的内部,而您所做的只是移动一个指针
*temp\u word
指向
*第一个
,然后指向
*第二个
,即
单词[50]的内容
未在中进行深入更改。您可以使用
memcpy
更改内容。

使用
strncpy
strdup
的适当实现是:

#include <string.h>

void swap_nodes( struct node *first, struct node *second ) {
    int temp_count;
    char *temp_word;

    temp_count = first->count;
    temp_word = strdup (first->word);
    first->count = second->count;
    strncpy (first->word, second->word, 50);    /* 50 based on struct definition       */
    second->count = temp_count;                 /* could be ( strlen (temp_word) + 1 ) */
    strncpy (second->word, temp_word, 50);

    if (temp_word)                              /* free memory allocated with strdup   */
        free (temp_word);
}
#包括
无效交换节点(结构节点*第一,结构节点*第二){
int temp_计数;
字符*临时字;
临时计数=第一次->计数;
临时单词=strdup(第一个->单词);
第一次->计数=第二次->计数;
strncpy(第一个->单词,第二个->单词,50);/*50基于结构定义*/
second->count=temp\u count;/*可以是(strlen(temp\u word)+1)*/
strncpy(第二个->单词,临时单词,50);
if(temp_word)/*使用strdup分配的可用内存*/
免费(临时单词);
}

使用
strncpy
strdup
的适当实现是:

#include <string.h>

void swap_nodes( struct node *first, struct node *second ) {
    int temp_count;
    char *temp_word;

    temp_count = first->count;
    temp_word = strdup (first->word);
    first->count = second->count;
    strncpy (first->word, second->word, 50);    /* 50 based on struct definition       */
    second->count = temp_count;                 /* could be ( strlen (temp_word) + 1 ) */
    strncpy (second->word, temp_word, 50);

    if (temp_word)                              /* free memory allocated with strdup   */
        free (temp_word);
}
#包括
无效交换节点(结构节点*第一,结构节点*第二){
int temp_计数;
字符*临时字;
临时计数=第一次->计数;
临时单词=strdup(第一个->单词);
第一次->计数=第二次->计数;
strncpy(第一个->单词,第二个->单词,50);/*50基于结构定义*/
second->count=temp\u count;/*可以是(strlen(temp\u word)+1)*/
strncpy(第二个->单词,临时单词,50);
if(temp_word)/*使用strdup分配的可用内存*/
免费(临时单词);
}

好吧,您已经收到了答案,我只想指出,您可以交换列表中的节点位置(而不是节点内容)。由于只有一个链接列表,因此需要节点的父节点来执行此操作


或者,您可以使用动态内存代替“word”的静态数组,这样您只需交换指针,避免数组副本。

您已经收到了答案,我只想指出,您可以交换列表中的节点位置(而不是节点内容)。由于只有一个链接列表,因此需要节点的父节点来执行此操作


或者,您可以使用动态内存来代替“word”的静态数组,这样您只需交换指针,避免数组副本。

您是否可以提供一些指示,说明您看到的是您不希望看到的行为,或者您希望看到的是您不希望看到的行为?
char-word[50]
是数组,无法分配,您需要通过
strcpy
memcpy
复制字符数组。您是否可以提供一些指示,说明您看到的是您不希望看到的行为,或者您希望看到的是您不希望看到的行为?
字符字[50]
是数组,无法分配,您需要通过
strcpy
memcpy