I need an array of user structs.
struct user {
char *username;
};
struct user users[10]; //Array of user structs
int main(int argc, char **args) {
int initUsersArray();
char *username = "Max";
addToUsersArrry(username);
}
int addToUsersArrry(username) {
int i;
i = 0;
struct user tmp;
for(i;i<10;i++) {
if(users[i] != NULL)
if(strcmp(*users[i].username,username)==0)
return -1;
}
i = 0;
for(i;i<10;i++) {
if(users[i] = NULL) {
users[i]=tmp;
users[i].username=username;
return 1;
}
}
}
int initUsersArray() {
int i;
i=0;
struct user tmp;
for(i;i<10;i++) {
users[i] = (struct user*) calloc(1,sizeof(tmp));
}
}
My first question is, if it is the right way to init the users array with NULL like i did.
The second problem is, that
*users[i].username
and other parts of code where want to get/set the user at a specific position, dont work.
Regards
Here, I fixed it for you. And don’t forget to diff and learn something. Don’t just c/p it.