I'm developing a client/server chat using C for a University project.
Everything is going fine, but i cannot fix this problem.
I created a struct to store the sockets of the two clients who have to communicate:
typedef struct pairing_Clients{
int client1fd;
int client2fd;
} pairedClients;
When i use it in a function that manages the communication between two clients, it gives me segfault. I tried with the following lines:
- Declared it as a global variable:
pairedClients *clients;
- Then, doing this gives me the segfault:
void client_handler(void *p_client) {
pairedClients *clients = (pairedClients *)malloc(sizeof(pairedClients));
clients->client1fd = 15;
}
It seems strange to me. I don't see any big problem. Any help?
malloc()in CNULLcheck. If you inspect the variables, doesclientscontain a valid address?main()function and at least one#include. Please edit your code so it's a minimal reproducible example of your problem (including any necessary inputs, but preferably not needing any), then we can try to reproduce and solve it. You should also read How to Ask.