Help with linked list in C


 
Thread Tools Search this Thread
Top Forums Programming Help with linked list in C
# 15  
Old 03-31-2011
Quote:
Originally Posted by omega666
the use of head was from an example, but i don't understand why its being done
a NULL pointer means end-of-list, right? So an empty list is nothing but a NULL pointer.
Quote:
and why my function isn't working the same way (without using head)
You weren't doing the same thing because you allocated and used memory when I didn't. I'll try again to illustrate why you don't need to allocate memory for the purpose of storing nothing:
Code:
# This isn't C code, I'm trying to show what pointers are pointing to what memory.
# The numbers are also totally made up.  They could be anything malloc()
# and your compiler give you.  The point is that pointers are just a special
# kind of number.

# What you did originally, declaring head without setting it to anything.
# When you tried to write to ????????????, the program crashed.
Address          Variable
0x100000000    head=0x????????????
0x??????????     INVALID MEMORY

# What I suggested instead.
# You still can't write to head->next since the pointer's invalid,
# but at least you know the memory's invalid.  You can use this
# to mean an empty list.
Address          Variable
NULL              INVALID MEMORY
0x100000000    head=NULL

# What a list with one node in it would look like.
# 0x100000010 would be an address you got from malloc().
# 0x000005000 might be the address the C compiler substitutes
# for a literal string like "quoted string".
Address           Variable
NULL               INVALID MEMORY
0x000005000    "client name"
0x100000000    head=0x100000010
0x100000010    client_name=0x000005000, next=NULL, fd=5

# What a list with two nodes in it would look like.
# 0x100000010 and 0x100000020 would be addresses you got from malloc().
Address           Variable
NULL               INVALID MEMORY
0x000005000    "client name"
0x000005010    "client 2"
0x100000000    head=0x100000010
0x100000010    client_name=0x000005000, next=0x100000020, fd=5
0x100000020    client_name=0x000005010, next=NULL, fd=6

Pointers never hold anything except an address -- a number meaning a place in memory.

When you left head unset, it ended up as some garbage ???????????. When you try to write anything inside ????????????, your computer knows you don't have access to ???????????? and kills your program.

NULL is just a zero. Your program never has access to memory address zero so C programs use it as a marker that means "this pointer doesn't go anywhere". So an empty linked list, having no nodes at all, is a head pointer that goes nowhere.

So when you feed a NULL head into your add_client program, you're just feeding it a zero. And when you feed another node into your add_client program instead, you're just giving it a 0x100000010 or whatever. The pointer doesn't do anything but hold that number. What's different is how you use these numbers.

Last edited by Corona688; 03-31-2011 at 06:53 PM..
# 16  
Old 03-31-2011
i tried changing it to
Code:
    client *current, *head=NULL;
    current = add_client("tttttttt", current, head, client_socket_fd);
    current = add_client("ttttttttt", current, head, client_socket_fd);
    printf("HERE\n");
    write(1,current->client_name,9);
    //printf("\n");
    //write(1,current->next->client_name,8);
    printf("\nHERE\n");

Code:
client *add_client(char *username, client *client_list, client *head, int client_socket_fd) {
    client *new_client = (client *)malloc(sizeof(client)); 
    new_client->client_name = username;
    new_client->client_socket_fd = client_socket_fd;
    new_client->next = head;
    head=new_client;
    client_list->next=head;
    return client_list;
}

and still segmentation fault

edit: nvm i think i see why this is wrong
# 17  
Old 03-31-2011
Stop flailing around and read what I'm writing for you. You don't understand how pointers work yet, until you do, writing code that works is going to be a matter of chance.

Trying what I suggested for you pages ago would also be nice.

I'll put it all together for you even.

Code:
    typedef struct client_list {         
        char *client_name;         
        struct client_list * next;         
        int client_socket_fd;     
    } client;
    client *current=NULL;
    int client_socket_fd=3;
    current = add_client("test", current, client_socket_fd);
    printf(current->client_name);

Code:
client *add_client(char *username, client *head, int client_socket_fd) {
    client *current = (client *)malloc(sizeof(client)); 
    current->client_name = username;
    current->next = head;
    current->client_socket_fd = client_socket_fd;
    return current;
}

# 18  
Old 03-31-2011
ok i think i got it working now with
Code:
    client *current;
    current = add_client("tttttttt", current, client_socket_fd);
    current = add_client("ttttttttt", current, client_socket_fd);
    printf("HERE\n");
    write(1,current->client_name,9);
    printf("\n");
    write(1,current->next->client_name,8);
    printf("\nHERE\n");

Code:
client *add_client(char *username, client *client_list, int client_socket_fd) {
    client *new_client = (client *)malloc(sizeof(client)); 
    new_client->client_name = username;
    new_client->client_socket_fd = client_socket_fd;
    new_client->next = client_list;
    return new_client;
}

do you see anything still wrong?
# 19  
Old 03-31-2011
Smilie

Code:
   client *current;
    current = add_client("tttttttt", current, client_socket_fd);

Code:
# What you did originally, declaring head without setting it to anything.
# When you tried to write to ????????????, the program crashed.
Address          Variable
0x100000000    current=0x????????????
0x??????????     INVALID MEMORY

# What your last post does
Address          Variable
0x000005000    "ttttttttt"
0x000005010    "tttttttttt"
0x100000000    current=0x100000010
0x100000010    client_name=0x000005000, next=0x100000010, fd=9
0x100000020    client_name=0x000005010, next=??????????, fd=8
0x??????????     INVALID MEMORY

If you try to read beyond the second node, you'll use the invalid memory ??????????? and it will crash. Because it's not NULL, you can't even tell that it's invalid. You want this:

Code:
# What your last post does
Address          Variable
NULL              INVALID MEMORY
0x000005000    "ttttttttt"
0x000005010    "tttttttttt"
0x100000000    current=0x100000010
0x100000010    client_name=0x000005000, next=0x100000010, fd=9
0x100000020    client_name=0x000005010, next=NULL, fd=8

And you accomplish that with client *current=NULL;
# 20  
Old 03-31-2011
well using the code i just put above, i got
HERE
ttttttttt
tttttttt
HERE
as the output, but when i use this function
Code:
int already_there(client *current, char *username) {
    //printf("OK\n");
    while(current) {
        if (strcmp(current->client_name, username)==0) return(1);
        //if (current->next==NULL) break;
        //else 
        current = current->next;
    }
    return(0);
}

it doesnt crash, but i dont get the right result
# 21  
Old 03-31-2011
Quote:
Originally Posted by omega666
well using the code i just put above
It ought to work, if you change "client *current;" into "client *current=NULL;" as I suggested above. Correct that code as I suggested above and try it again.

And if it still doesn't work, some values you're feeding into it are wrong so I will need you to post the complete program to tell why.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

How to reverse a linked list by traversing only once.?

Program to reverse a linked list by traversing only once. (1 Reply)
Discussion started by: VSSajjan
1 Replies

2. Programming

How to delete the last node in a linked list.?

How to delete the last node in a single linked list given only the pointer to last node ? Head node will not be given. (5 Replies)
Discussion started by: VSSajjan
5 Replies

3. Programming

Help with linked list.

#include<stdio.h> #include<stdlib.h> struct LinkedList { int val; struct LinkedList *next; }node; /*Creating a structure variable*/ typedef struct LinkedList Node; Node *start = NULL; int create(int i) { Node *temp = NULL; if (start == NULL) ... (5 Replies)
Discussion started by: prinsh
5 Replies

4. UNIX for Advanced & Expert Users

Unix linked-list placement

Hi, I am programming in kernel, and I want to use a double linked list that holds infos that every process could access and modify THIS list. So, I suppose it is a 'global' variable since every process(thread) can reach it, I am wondering where to put it? by changing some of the kernel files? (1 Reply)
Discussion started by: louisTan
1 Replies

5. Programming

how to check if something exists in a struct linked list?

can someone provide an example of a struct linked list, where it has strings as its values, and then how do I check if a specific string (say called buffer) exists in the list of structs? i dont understand how to make a copy of it to check with this is what i have ... (0 Replies)
Discussion started by: omega666
0 Replies

6. Programming

How to check if something exists in linked list in C?

i have a linked list set up like typedef struct client_list { char *client_name; int client_socket_fd; struct client_list *next; } client; client *client_list=NULL; before adding to the list i check if it already exists, only if it does not then i add if (client_list==NULL... (1 Reply)
Discussion started by: omega666
1 Replies

7. Programming

I need C++ Code for single linked list

I need C++ Code for single linked list With operations as 1)insert at any position 2)delete any 3)change the data of any position (2 Replies)
Discussion started by: girija
2 Replies

8. Programming

shared memory with linked list??

is this possible, if so plz please share with me.. Correct English please, not Cyber-/Leetspeak (11 Replies)
Discussion started by: vijay_manpage
11 Replies

9. UNIX for Dummies Questions & Answers

List linked files

A perl script that displays the list of files which have multiple links..! ls -l shows number of links in a field. (0 Replies)
Discussion started by: aadi_uni
0 Replies

10. Programming

Reverse single linked list

Can any one help me in reversing the single linked list and at the same time i want to print the reversed links. (2 Replies)
Discussion started by: dhanamurthy
2 Replies
Login or Register to Ask a Question