English 中文(简体)
穿过堆积层
原标题:Passing pointer to array of struct

我试图把一个点推向一系列的构件。 这部法典应当制造一系列的构件,写上斜体中的斜体,然后打印出(正在运行)。 然后,我想把这一系列的 st子传递给另一个功能,并打印出各种休战。

#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
", clients[i].client_number=i, clients[i].client_name);
  }

  printf("

");
  pass_func(&clients);
}

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

这是产出:

$ 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

我做了大约一小时的研究,可以说明为什么不这样做。 我发现的大多数实例是C++,但不是C。 (我知道我所包括的许多头号档案对这项法典并不必要;这只是我原法典的一部分)。)

最佳回答

<代码> 绕行-func期望有一系列点人到<>条码>。

void pass_func(client* clients[]);

页: 1

pass_func(&clients);

a 一系列的<代码>客户。 因此,<代码>client/code>clients[i] is interpret as a pointer to clientin> in pass_func,但当然,借方不是client/code>的有效联络点,因此,您试图查阅您的笔记,并获取缩略号。

要么通过一系列要点,要么宣布<代码>

void pass_func(client *clients);

(然后通过<代码>pass_func(客户),没有主机地址。)

您的编辑们警告你,尽管如此,他们却采取了一种不兼容的点。

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

罚款。

你们需要获得基本权利......

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





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...