0
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>


typedef struct user {
    char *username;
    struct sockaddr_in addr;
} user;

user* users;

typedef struct room
{
  char *roomname;
  user* users;
} room;

room* rooms;

int addToUsersArray(char *username) {
    int i = 0;

    for(; i<10; i++) {
            if(users[i].username=='\0') {
                    users[i].username = username;
                return 1;
            } else if(strcmp(users[i].username, username) == 0)
                return -1;
    }
    return -1;
}


void initUsersArray() {  
    users = (user*) calloc(10, sizeof(user)); 
}

void initRoomsArray() {  
rooms = (room*) calloc(10, sizeof(room));
    int i =0;   
    for(;i<10;i++)      
         rooms[i].users = (user*) calloc(10,sizeof(user));  
}

int addToRoomsArray(char *roomname) {
    int i = 0;

    for(; i<10; i++) {
            if(rooms[i].roomname=='\0') {
                    rooms[i].roomname = roomname;
                return 1;
            } else if(strcmp(rooms[i].roomname, roomname) == 0)
                return -1;
    }
    return -1;
}

int addUserToRoom(char *roomname, user usr) {
    int i = 0;
    int k = 0;

    for(; i<10; i++) {
            if(rooms[i].roomname=='\0') {
                    rooms[i].roomname = roomname;
                    return 1;
            } else if(strcmp(rooms[i].roomname, roomname) == 0) {
            for(;k<10;k++) {
                if(rooms[i].users[k].username==NULL) { //This line makes trouble
                    rooms[i].users[k] = usr;
                }
            }
        }

    }
    return -1;
}


int main(int argc, char** argv) {
    initUsersArray();  
    initRoomsArray();      
    char *username = "Max";
    addToUsersArray(username);
    username = "Ma1x";
    addToUsersArray(username);
    printf("%s\n",users[0].username);
    printf("%s\n",users[1].username);

struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(4444);
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");  

char *room = "sportchat";
addToRoomsArray(room);
room = "berlinchat";
addToRoomsArray(room);

    printf("%s\n",rooms[0].roomname);
    printf("%s\n",rooms[1].roomname);

user michi;
michi.username = "michi";
michi.addr = addr;

struct sockaddr_in addr2;
    addr2.sin_family = AF_INET;
    addr2.sin_port = htons(1234);
    addr2.sin_addr.s_addr = inet_addr("127.0.1.1");

user willi;
willi.username = "willi";
willi.addr = addr2;

addUserToRoom(room,michi);
addUserToRoom(room,willi);


    return 1;
}

运行 addUserToRoom(room,michi) 时出现分段错误。我仍然有点不确定使用结构。注释掉rooms[i].users[k] = usr;分段错误时消失。我在 unix 系统上使用 gcc。gcc 是否分析带有空块的 if 表达式?

编辑 我如何实现可变数量的用户和房间?我想我必须使用 realloc。但是怎么做?问候

4

2 回答 2

2

你永远不会分配内存,也不会初始化

room.users

因此,当您尝试访问 rooms[i].users[k] 时,您会遇到分段错误。

作为旁注:永远不要使用全局变量,否则您会做愚蠢的事情(当您使用与结构成员同名的全局变量时更是如此)。此外,始终在 for 循环的第一部分初始化您的迭代变量。(你知道人们死于少于那个吗?:P)

这是您的主要功能的示例:

int main() {
    room* rooms;
    user* users;

    initUsersArray(users);
    initRoomsArray(rooms);

    char *username = "Max";
    addToUsersArray(users,username);
    username = "Ma1x";
    addToUsersArray(users,username);
    printf("%s\n",users[0].username);
    printf("%s\n",users[1].username);

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(4444);
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");

    char *room = "sportchat";
    addToRoomsArray(rooms,room);

    /* ... */

    return 1;
}
于 2012-05-19T16:21:22.357 回答
0

该代码有几个问题。要回答这个问题,您还没有分配rooms[i].users数组。

于 2012-05-19T16:25:25.690 回答