Nested structures :S?
Hi,
I'm trying to code a simpel IRC bot (just for fun, so I can learn how to use strings/arrays in C). I have this at the moment in my header: Code:
struct STRUCT_AUTH { auths[lastauth].nick=nickname and auths[lastauth++].authname =authname. Oke, all that works. Perfect. Now my problem, I want to make something like that, except a nested structure (if I call that correct) Some people helped me and I understood something like this: Code:
struct user { Some people gave me some malloc() examples, except they didn't work. I want to be able to do something like this: /* Here the malloc() thing and make a pointer c and a linkg c->user_list */ c->channame[x] = channel, c->activeusers=8 c->user_list[i]->nick=nick1, c->user_list[i]->nick=nick2, c->user_list[i]->nick=nick3, etc... c->channame[x++] = channel, c->activeusers+=4 c->user_list[i]->nick=nick1, c->user_list[i]->nick=nick2,etc... (Btw, I want multi-channeling as well, as you see) Greetz, AliTriX Please, if you give me URL's wher they explain these stuff, give me a "good" url that explains something like that and not how to make arrays that tell you the x-y stuff (graphics, I don't understand a shit of that subject anyway) |
There's something wrong with the structure you use. If I see it correctly, you use one table (channame) to keep different channel names (only offset is different). I think it should be something like this:
Code:
struct user { Code:
struct user *userptr; /* pointer for user */ The code above doesn't have this problem. |
Hm...
I tryed to edit to change a little stuff, but there are several lines I get warnings/errors from and my compiler didn't make a binary from, cause the erros. The code that I have: Code:
#include <stdio.h> Errors: dbzgt@AliTriX:~/c-test/trixy/tests$ gcc test.c -o test test.c: In function `main': test.c:22: incompatible types in assignment test.c:24: warning: assignment makes pointer from integer without a cast test.c:25: incompatible types in assignment test.c:29: warning: assignment makes pointer from integer without a cast test.c:32: incompatible types in assignment (Line numbering is exactlly the same as it's pasted here) |
Yes, I made a mistake. For copying strings, you can't use =. You need strcpy function. I also added casting - '(struct user *)malloc'. It tells that the result of malloc (free memory, without structure) should be threated as user structure.
Here's the code after corrections. Code:
#include <stdio.h> |
K, tnx Mara, it all works.
Let say, I want to add a lot of users, and want to make a loop. What part of that malloc do I have to put in the loop? And how to read all the users in from a channel? (I hope I'm not irritating you with these questions) |
Well, you can use two ways to add users. At the beginning of the list or at the end. First option is simplier:
Code:
struct user *newuser; The code to list them all: Code:
struct user *usr=channel[0].user_list; |
Thanks!
/me hugs Mara |
Good luck writing your program, alitrix. :)
|
Tnx again.
I hope it will work fine all of it :) And if it doesn't, I just state my questions here ;) ... If not bothering :P |
Sorry, but I just have 1 question.
I want to change that structure as well, so it can take more users then MAXUSERS defines. I was trying to use with *next as well, but couldn't really figure out. Code:
struct auths { Code:
struct auths *newuser; |
The code is nearly OK.
What I propose Code:
struct auths *newuser; |
Thanks for you reply :)
But I forgot to tell you that I've got it already and I know understand a little bit more about linked structures and nested structures. I'm making big structures now, tnx for helping me with everything! |
All times are GMT -5. The time now is 11:09 PM. |