Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7960563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T04:47:29+00:00 2026-06-04T04:47:29+00:00

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

  • 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;
}

When running addUserToRoom(room,michi) i get a segmentation fault. I am still a little bit unsure with using structs. When commenting out rooms[i].users[k] = usr; the segmentation fault disappears. I am using gcc on an unix system. Does gcc analyse an if-expression with an empty block?

EDIT
How do i implement a variable number of users and rooms? I think i have to use realloc. But how?
Regards

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T04:47:30+00:00Added an answer on June 4, 2026 at 4:47 am

    you never allocate memory, neither initialize

    room.users
    

    so when you try to access rooms[i].users[k] you get a segmentation fault.

    as a side note: DON’T EVER USE GLOBALS, or you’ll do stupid stuff (even more when you’re using globals that have the same name as members of your structs). Also, always initialize your iteration variables in first part of for loops. (Do you know people died for less than that ? :P)

    here is an example of what shall be your main function:

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is the code #include<stdio.h> #include<sys/types.h> #include<stdlib.h> #include<pthread.h> typedef struct std_thread { char name[20];
Client code: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <sys/types.h> #include <sys/socket.h>
Here is my server program #include<stdio.h> #include<stdlib.h> #include<sys/socket.h> #include<sys/un.h> #include<sys/types.h> #include<unistd.h> int main ()
#include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<sys/ipc.h> #include<sys/msg.h> #include<sys/types.h> struct mbuffer{ long mtype; int mtext; }stResult; int
Here is my code: #include<stdio.h> #include<stdlib.h> #include<sys/stat.h> #include<sys/types.h> #include<fcntl.h> #include<unistd.h> #include<errno.h> int main(int argc,char
This is my chat server : #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/types.h>
Have a look at the following code: #include<unistd.h> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<sys/types.h> main() {
When I try to use exec: #include <stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h>
Here's a program I'm trying to make: #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include
I am trying to understand the following code: #include<stdio.h> #include<stdlib.h> #include<sys/io.h> #define baseport 0x378

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.