简体   繁体   English

C链表-指针值更改

[英]C Linked list - value of pointer changing

So I'm fairly new to C and pointers and I have a project for school to finish functions for adding to a linked list 因此,我对C和指针还不陌生,我有一个项目供学校完成添加到链表中的功能

this is how it looks: 它是这样的:

TEMPLOYEE * newEmployee(const char * name, TEMPLOYEE * next)
{
    TEMPLOYEE* head = NULL;
    head = malloc(sizeof(TEMPLOYEE));
    if(head==NULL)
    {
        return 1;
    }

    head -> m_Name = name;
    head -> m_Next = next;
    head -> m_Bak = NULL;
}

this works when entering name as a string for example a = newEmployee ( "Peter", a ); 这在以字符串形式输入名称时有效,例如a = newEmployee ( "Peter", a ); but when I try to add using a temporary value the m_Name in like this 但是当我尝试使用一个临时值添加m_Name像这样

strncpy ( tmp, "Victoria", sizeof ( tmp ) );
a = newEmployee ( tmp, a );
strncpy ( tmp, "Peter", sizeof ( tmp ) );
a = newEmployee ( tmp, a );

the list will change with the value so it will have two employees with the name Peter instead of Peter and Victoria and I can't find how to do this. 列表将随着值的变化而变化,因此它将有两名雇员,名为Peter,而不是Peter和Victoria,我找不到如何做的方法。 Any help is welcome. 欢迎任何帮助。

In the function newEmployee() , you are doing: 在函数newEmployee() ,您正在执行以下操作:

head -> m_Name = name;

In both newEmployee() call, you are passing tmp : 在两个newEmployee()调用中,您都传递了tmp

a = newEmployee ( tmp, a );
a = newEmployee ( tmp, a );

So, the name pointer of both the nodes are pointing to same location tmp which contains the name. 因此,两个节点的name指针都指向包含名称的相同位置tmp If you make any change in the value of tmp , it will be reflected in both. 如果您对tmp的值进行了任何更改,它将同时反映在两者中。

The in memory view would be something like this: 内存视图如下所示:

---------------------
|m_Name|m_Next|m_Bak|
---------------------
    | 
    \/    tmp
   -----------
   | Peter   |
   -----------
    /\
    |
---------------------
|m_Name|m_Next|m_Bak|
---------------------

// Both the node m_Name is pointing to same location.
// Make any change in tmp and the changes will reflect in both the pointers
// as they are pointing to same location.

To solve this problem, you should allocate memory to m_Name and copy the tmp to it. 要解决此问题,您应该将内存分配给m_Name并将tmp复制到其中。 You can use strdup() to resolve this issue. 您可以使用strdup()解决此问题。 In the newEmployee() , replace this: newEmployee() ,替换为:

head -> m_Name = name;

with this: 有了这个:

head -> m_Name = strdup(name);

The strdup() function returns a pointer to a new string which is a duplicate of the string passed to it. strdup()函数返回一个指向新字符串的指针,该指针与传递给它的字符串的副本相同。 strdup() allocates a memory for the new string and returns its pointer. strdup()为新字符串分配一个内存,并返回其指针。 Make sure to free it using free() once you are done with it. 完成后,请确保使用free()它。 Note that strdup() is not part of the C Standard. 请注意, strdup()不属于C标准。

The newEmployee() function return type is TEMPLOYEE * but you are not returning anything from this function if the malloc is successful. newEmployee()函数的返回类型为TEMPLOYEE *但如果malloc成功,则不会从此函数返回任何内容。 Also, if malloc fails for some reason you are returning integer constant 1 from it which is incorrect. 另外,如果malloc由于某种原因而失败,那么您将从中返回不正确的整数常量1 Instead, you should return NULL in case of failure. 相反,如果失败,您应该返回NULL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM