Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing pointer to array of struct

Tags:

arrays

c

struct

I'm trying to pass a pointer to an array of struct. This code should create an array of struct, writes to the vars in struct, and then prints them out (which works). Then I want to pass a pointer of that array of struct to another function and print out the array of struts.

#define PORT_NUMBER 5100
#define MAX_CLIENTS 5

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <pthread.h>

typedef struct thread_args
 {
    int client_number;
    int connected;
    char client_name[1024];
} client;

void pass_func(client* clients[])

int main()
{
  struct thread_args clients[MAX_CLIENTS];
  int i;

  for(i =0; i < MAX_CLIENTS; i++)
  {
  clients[i].client_number=i;
  strcpy(clients[i].client_name, "BOBBY");
  }

    for(i =0; i < MAX_CLIENTS; i++)
  {
     printf("%d | %s\n", clients[i].client_number=i, clients[i].client_name);
  }

  printf("\n\n");
  pass_func(&clients);
}

void pass_func(client* clients[])
{
  int i;
  for(i =0; i < MAX_CLIENTS; i++)
  {
     printf("%d | %s\n", clients[i]->client_number=i, clients[i]->client_name);
  }
}

And this is the output:

$ gcc TEST.c -lpthread -o TEST.out
TEST.c: In function ‘main’:
TEST.c:41:3: warning: passing argument 1 of ‘pass_func’ from incompatible pointer type [enabled by default]
TEST.c:22:6: note: expected ‘struct thread_args **’ but argument is of type ‘struct thread_args (*)[5]’

$ ./TEST.out 
0 | BOBBY
1 | BOBBY
2 | BOBBY
3 | BOBBY
4 | BOBBY


Segmentation fault

I've done about an hour of research and can't figure out why this is not working. Most of the examples I find are for C++, but not C. (And yes I know many of the header files I've included are not necessary for this code; this is just a segment of my original code.)

like image 944
faction918 Avatar asked Dec 05 '22 16:12

faction918


2 Answers

pass_func expects an array of pointers to client

void pass_func(client* clients[]);

but you pass it

pass_func(&clients);

a pointer to an array of clients. So the client clients[i] is interpreted as a pointer to client in pass_func, but of course the bit-pattern is not a valid pointer to client, hence you're trying to access memory you shouldn't and get a segfault.

Either pass an array of pointers, or declare pass_func

void pass_func(client *clients);

(and then pass pass_func(clients) without the address-operator in main).

Your compiler warned you about passing an incompatible pointer type, though.

like image 153
Daniel Fischer Avatar answered Dec 21 '22 02:12

Daniel Fischer


void pass_func(client* clients[])
{
  int i;
  for(i =0; i < MAX_CLIENTS; i++)
  {
     printf("%d | %s\n", (*clients)[i].client_number=i, (*clients)[i].client_name);
  }
}

this will be fine.

like image 39
bruce Avatar answered Dec 21 '22 03:12

bruce