添加一个新用户列出在C程序 [英] adding a new user to list in c program

查看:188
本文介绍了添加一个新用户列出在C程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想写一个简单的函数朋友ADDD到UserAccount列表。所有的信息通过参数设置。如果用户已经在列表中我并不需要重新添加他,但显示的记录说,他已经在列表中。我写这片code的。不知道这是正确的,任何建议提高code?请问这项工作?

  INT add_friend(UserAccount *用户,焦圈,UserAccount *的朋友)
{
    结构UserAccountNode * P;    P =(结构UserAccountNode *)malloc的(的sizeof(结构UserAccountNode));    而(P!= NULL)
        如果(stricmp(用户,P->朋友)== 0){            P->接下来=头; //在开头插入
            头= P;
        }
        其他{            的printf(%d个已经存在,朋友)
        };
}


解决方案

不知道这是正确的 - 它做你想要它做什么?如果是这样,它的可能的是正确的;如果不是。嗯......

不,它无处的附近的正确。事情的清单错误做出像样的情况下,为废弃的这一切。这样的事情,包括(但不限于):


  • 您声称要有条件地添加新的朋友,但只有当他们是不是已经在列表中。然而,的你先做的事情是什么东西分配空间你甚至不知道你需要吗?


  • 您回路有可能能满足没有退出条件(除非的malloc()实际的失败的)。在任何时候为 P ,唯一的退出条件的说法,曾经的分配到的任何的函数中的第一行之后,它分配一个动态分配你可能甚至不需要。即你有一个无限循环。


  • 您正在传递一个 UserAccount * stricmp 作为第一个参数,它需要一个为const char *


  • P-GT和比较;朋友用户。但是您只需拨什么 P 点。及其朋友成员是不确定的,即具有没有明确的内容,但你要发送的不确定内容 stricmp()来比较的输入参数用户。这将调用的未定义行为即可。


  • 的比较逻辑是倒退。 stricmp()返回0,如果字符串是不区分大小写的等于;没有什么不同。你的逻辑(即使你没有因上述的现有项目调用不确定的行为)至少的尝试的添加项目只有当他们已经​​在列表present。


  • 在列表中两个迭代后,如果出现奇迹了,如果-EX pression评估为true两次,你已经创建了一个圆形的自引用节点和孤儿,你原本的深渊任何列表。


  • 您可以发送朋友,一个 UserAccount * ,到的printf %D格式说明。虽然这的可能的不会的崩溃的程序,它是无 - 少更不确定的行为。如果你要打印与的printf()使用%P


  • 未使用的函数参数是老老实实的至少的您的后顾之忧。他们可能没有被使用,但在好的方面,他们也都没有使用的错误的;东西不能用户可以说和朋友


我不会对你照关于好功夫或不错的尝试。有没有机会,这code连的编译的,绝对的的机会,它可能的曾经的正常运行。您需要检查你试图实现实际的算法和的指针和用C动态内存复习使用。

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)
        };
}

解决方案

"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.

这篇关于添加一个新用户列出在C程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆