简体   繁体   中英

Passing pointer to array of 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.)

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 client s. 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.

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.

You need to get the basics right...

You need to first understand how arrays are passed to a function first: better go through this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM