如何从C中的结构中的结构获取char数组

时间:2015-06-06 23:03:35

标签: c pointers struct

我定义了以下结构:

在gf.h中:

typedef struct gfcrequest_t gfcrequest_t;

在gf.c中:

typedef struct gfcrequest_t {
  struct sockaddr_in saddr;
  // ...
  char *path; // <---- get here
  gfstatus_t status;
  void (*writefunc)(void*, size_t, void *);
  // ...
} gfcrequest_t;

在gf_main.c

typedef struct wrequest_t {
  long int msg_type;
  char *message;
  gfcrequest_t *gfr; // <---- from here
} wrequest_t;

我希望能够访问path实例上gfr指针的wrequest_t成员。

我有以下内容:

wrequest_t *wrequest = malloc(sizeof(struct wrequest_t));
bzero(&wrequest, sizeof(wrequest));
gfcrequest_t *gfr = gfc_create(); // function returns a pointer to gfcrequest_t
// ... some pre-processing
wrequest->gfr = gfr; // pointer to pointer, is that correct?
// ... pass the wrequest to thread through message queue for processing

然后,在线程中,我将请求弹出队列并尝试处理它

  wrequest_t *wrequest;
  for (;;) {
    if (msgrcv(msg_id, &wrequest, size_wrequest, MSG_TYPE, 0) == -1) {
      perror("failed to receive work request");
      return NULL;
    }
    fprintf(stderr, "message is %s\n", wrequest->message);

    if (strncmp(wrequest->message,
                SHUTDOWN_MESSAGE, strlen(SHUTDOWN_MESSAGE)) == 0) {
      fprintf(stderr, "thread %lu is done\n", id);
      break;
    }

    fprintf(stderr, "thread %lu would process %s\n", id, wrequest->gfr->path); // <---------------------- this line doesn't work
  }

编译抱怨:\

cc -Wall --std=gnu99 -g -Wno-format-security   -c -o gfclient_download.o gfclient_download.c
gfclient_download.c: In function ‘work’:
gfclient_download.c:125:71: error: dereferencing pointer to incomplete type
     fprintf(stderr, "thread %lu would process %s\n", id, wrequest->gfr->path);

如果我按->更改.,则会抱怨:

cc -Wall --std=gnu99 -g -Wno-format-security   -c -o gfclient_download.o gfclient_download.c
gfclient_download.c: In function ‘work’:
gfclient_download.c:125:71: error: request for member ‘path’ in something not a structure or union
     fprintf(stderr, "thread %lu would process %s\n", id, wrequest->gfr.path);

我是C的新手你可以看到......有什么明显的事我做错了吗?如何从path转到wrequest_t

0 个答案:

没有答案