ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I understand that. When I want to add somebedy to the auth-structure, I just do:
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 {
char nick[20]; /* User nick that is on channel x */
};
struct channel {
char channame[40]; /* Channel name */
int activeusers; /* How many users are on the chan? */
struct user *user_list;
};
Oke, now my problem. How to declare this in my source (.c) file?
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...
(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 {
char nick[20]; /* User nick that is on channel x */
struct user *next; /* pointer for next user */
};
struct channel {
char channame[40]; /* Channel name */
int activeusers; /* How many users are on the chan? */
struct user *user_list;
};
struct channel channels[40];
And then
Code:
struct user *userptr; /* pointer for user */
channels[0].channame = something; /* channel name */
channels[0].activeusers=8;
channels[0].user_list=malloc(sizeof(struct user)); /* memory allocated for 'user' structure
channels[0].user_list->nick=nick1; /* filling name */
channels[0].user_list->next=NULL; /* null means there's nothing */
userptr=channels[0].user_list; /* now the pointer is channels[0].userlist (which points to
memory allocated by malloc) */
userptr->next=malloc(sizeof(struct user)); /* malloc assigns memory for user struct, it's
channel[0].user_list->next */
userptr=userptr->next; /*userptr becomes channel[0].user_list->next */
userpty->nick=nick2; /* filling new user structure */
userptr->next=NULL;
Your code can be also written without mallocs, using only tables. Byt there's one big BUT: it needs specified maximum number of users. If you set the maximum to 100, the program will use alwys memory for 100 users, even when there's only one user. It's not very bad for 100 users, but imagine 500 channels with 100 users each.
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>
#include <string.h>
struct user {
char nick[20]; /* User nick that is on channel x */
struct user *next; /* pointer for next user */
};
struct channel {
char channame[40]; /* Channel name */
int activeusers; /* How many users are on the chan? */
struct user *user_list;
};
struct channel channels[40];
int main() {
char channel[]="#test";
char nick1[]="AliTriX";
char nick2[]="AliTriX2";
struct user *userptr; /* pointer for user */
channels[0].channame = channel; /* channel name */
channels[0].activeusers=8;
channels[0].user_list=malloc(sizeof(struct user)); /* memory allocated for 'user' structure */
channels[0].user_list->nick=nick1; /* filling name */
channels[0].user_list->next=NULL; /* null means there's nothing */
userptr=channels[0].user_list; /* now the pointer is channels[0].userlist (which points to
memory allocated by malloc) */
userptr->next=malloc(sizeof(struct user)); /* malloc assigns memory for user struct, it's
channel[0].user_list->next */
userptr=userptr->next; /*userptr becomes channel[0].user_list->next */
userptr->nick=nick2; /* filling new user structure */
userptr->next=NULL;
}
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>
#include <string.h>
struct user {
char nick[20]; /* User nick that is on channel x */
struct user *next; /* pointer for next user */
};
struct channel {
char channame[40]; /* Channel name */
int activeusers; /* How many users are on the chan? */
struct user *user_list;
};
struct channel channels[40];
int main() {
char channel[]="#test";
char nick1[]="AliTriX";
char nick2[]="AliTriX2";
struct user *userptr; /* pointer for user */
/*channels[0].channame = "#test1"; channel name */
strcpy(channels[0].channame,channel);
channels[0].activeusers=8;
channels[0].user_list=(struct user *)malloc(sizeof(struct user)); /* memory allocated for 'user' structure */
/*channels[0].user_list->nick=nick1; filling name */
strcpy(channels[0].user_list->nick,nick1);
channels[0].user_list->next=NULL; /* null means there's nothing */
userptr=channels[0].user_list; /* now the pointer is channels[0].userlist (which points to
memory allocated by malloc) */
userptr->next=(struct user *)malloc(sizeof(struct user)); /* malloc assigns memory for user struct, it's
channel[0].user_list->next */
userptr=userptr->next; /*userptr becomes channel[0].user_list->next */
/*userptr->nick=nick2; filling new user structure */
strcpy(userptr->nick,nick2);
userptr->next=NULL;
printf("0: %s 1: %s\n",
channels[0].user_list->nick,
channels[0].user_list->next->nick);
}
struct auths *newuser;
newuser=(struct auths *)malloc(sizeof(struct auths)); /* memory allocated for 'user' structure */
strcpy(newuser,"Nick1");
//don't know what's authname is, probably login or something like that
//you should fill it here
newuser->next=channels[0].user_list;
channels[0].user_list=newuser;
I forgot about one thng - the variable you have the name of users in. It's a good idea to have it. The code will be simple - just increase it after adding user.
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!
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.