adding a new user to list in c program

随声附和 提交于 2019-12-24 22:03:34

问题


Im just trying to write a simple function to addd a friend to a UserAccount list. All the information is provided through the parameter. If the user is already in the list I do not need add him again but display records saying he is already in list. I wrote this piece of code. not sure if this is correct, any suggestion to improve the code? Does this work?

int add_friend(UserAccount* user, char Circle, UserAccount* friend)
{
    struct UserAccountNode *p;

    p = (struct UserAccountNode *) malloc(sizeof(struct UserAccountNode));

    while (p != NULL)
        if(stricmp(user, p->friend) == 0){

            p->next = head; // inserting at the beginning
            head = p;
        }
        else {

            printf("%d already exists", friend)
        };
}

回答1:


"not sure if this is correct" - does it do what you intended it to do? if so, it might be correct; if not. well...

And no, its nowhere near correct. the list of things wrong make a decent case for scrapping all of it. Such things include (but are not limited to):

  • You claim you want to conditionally add a new friend but only if they're not already in the list. Yet the first thing you do is allocate space for something you're not even sure you need yet?

  • Your loop has no exit condition that can possibly be met (unless malloc() actually fails). At no time is p, the only exit condition argument, ever assigned to anything after the first line in the function, which assigns it a dynamic allocation you may not even need. I.e. You have an infinite loop.

  • You're passing a UserAccount* to stricmp as the first parameter, which expects a const char*.

  • You're comparing p->friend with user. But you just allocated what p points to. Its friend member is indeterminate, i.e. has no defined content, yet you're sending that undefined content to stricmp() to compare against the input parameter user. This invokes undefined behavior.

  • The comparison logic is backwards. stricmp() returns 0 if the strings are case-insensitive equal; not different. You logic (even if you weren't invoking undefined behavior due to the prior item mentioned above) is at least attempting to add items to the list only if they're already present.

  • After two iterations through the list, if by some miracle the if-expression evaluated to true twice, you have created a circular self-referencing node and orphaned any list you originally had into the abyss.

  • You're sending friend, a UserAccount*, to printf with a "%d" format specifier. While this likely won't crash your program, it is none-the-less more undefined behavior. If you want to print a pointer value with printf() use "%p"

  • The unused function parameters are honestly the least of your worries. They may not have been used, but on the plus side, they likewise weren't used incorrectly; something that cannot be said for user and friend.

I'm not going to shine you on about "good effort" or "nice try". There is no chance this code even compiled, and absolutely zero chance it could ever run correctly. You need to review the actual algorithm you're trying to implement, and heavily review usage of pointers and dynamic memory in C.




回答2:


You code has at least two problems:

  1. while (p != NULL)

    If p is not NULL, this will be an infinite loop, because you do not change p in the loop body or use statements like break to jump out of it.

  2. stricmp(user, p->friend)

    You are using an uninitialized variable, this

    p = (struct UserAccountNode *) malloc(sizeof(struct UserAccountNode));
    

    only allocated a structure, but you never initialize it before you use it in the while loop.

Other mistakes are possible, such as you never use the friend argument, the parameters to stricmp() may be wrong, and so on.



来源:https://stackoverflow.com/questions/22627293/adding-a-new-user-to-list-in-c-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!