0

I am trying to add users to a linked list. I have two structs and a method to add called add_friend that sets up the addition of the node. The program does not ask for user input but passes the info through parameters in the add_friend method: In addition to adding the node (user) to the list I have to check if user already exists. I am getting an error when I try to compare string to see if the user exists. Any help? Unfortunately C is my weakest programming language, Im having a hard time understanding the pointers

struct UserAccountNode {
    struct UserAccount* content;
    char circle;
    struct UserAccountNode* next;

} *head = NULL;

struct UserAccount {
    char username[255];
    char lastnm [256];
    char firstnm[256];
    char password[256];
    char gender;
    int phone;
    struct Post* post_list;
    struct UserAccountNode* friend_list;
};

int add_friend(UserAccount* user, char Circle, UserAccount* Friend) {
    struct UserAccountNode* friend_list;

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

    while (friend_list != NULL)
        if (stricmp(Circle, head->friend_list) == 0) {
            friend_list -> next = head;
            head = friend_list;
        } else { 
            printf("%d, User Already Exists", Friend);
        }

    return 0;
}
2
  • 1
    If this is the aftermath of the dissection of this, you need to put down StackOverflow and pick up your text book. Seriously. Stop writing code and spend some time researching and reading it. There is no short-cut to this. Commented Mar 25, 2014 at 22:28
  • How do you want to determine if the friend is already on the list? Can you just check if a user with the same address is already present? Commented Mar 26, 2014 at 2:48

2 Answers 2

2

The code does not compare strings. It compares char - Circle - with UserAccountNode* friend_list. But stricmp requires both arguments to be const char *. You have to do a loop through all items in friend_list and compare every username with a given one.

Another issue: you allocate memory for UserAccountNode but do not allocate memory for its internal field UserAccount* content. It may crash the application when you try to read the data.

Sign up to request clarification or add additional context in comments.

Comments

2

type of Circle is char not char*, type of head->friend_list is UserAccountNode*.

So, you try to compare non-string objects as strings here:

if (stricmp(Circle, head->friend_list) == 0)

I think your program can't be compiled.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.