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