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:
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.
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:
And you accomplish that with client *current=NULL;
well using the code i just put above, i got
HERE
ttttttttt
tttttttt
HERE
as the output, but when i use this function
it doesnt crash, but i dont get the right result
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)