0

I am a newbie in ZooKeeper. My code is quite simple as shown below. However, I got the error dereferencing pointer to incomplete type when I want to get the file descriptor of zookeeper handle.

#include <stdio.h>
#include "zookeeper.h"

static zhandle_t *zh;
typedef struct String_vector zoo_string;

void my_watcher_func(zhandle_t *zzh, int type, int state, const char *path, void watcherCtx()) {}

int main(int argc, char *argv[]) {
    int i, retval;
    char *host_port = "localhost:2191";
    char *zoo_root = "/";
    zoo_string *children_list = (zoo_string *)malloc(sizeof(zoo_string));

    zh = zookeeper_init(host_port, my_watcher_func, 2000, 0, NULL, 0);
    if (zh == NULL) {
        fprintf(stderr, "Error connecting to zookeeper server!\n");
        exit(EXIT_FAILURE);
    }

    retval = zoo_get_children(zh, zoo_root, 0, children_list);
    if (retval != ZOK) {
        fprintf(stderr, "Error retrieving znode from path %s!\n", zoo_root);
        exit(EXIT_FAILURE);
    }

    fprintf(stderr, "\n=== znode string === [ %s ]\n", zoo_root);
    for (i = 0; i < children_list->count; ++i) {
        fprintf(stderr, "\n(%d): %s\n", i + 1, children_list->data[i]);
    }
    fprintf(stderr, "\n=== done ===\n");
    fprintf(stderr, "%d\n", zh->fd); //error

    zookeeper_close(zh);
    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
HuangJie
  • 1,488
  • 1
  • 16
  • 33
  • Hi @MohitJain thanks for your reply. `String_vector` comes from ZooKeeper header. – HuangJie Mar 17 '16 at 05:34
  • 7
    I know nothing about zookeeper. But the common convention is that any C pointer types with the word "handle" in them means that they are opaque and cannot be dereferenced. A google search for [zookeeper_init](http://zookeeper.sourcearchive.com/documentation/3.2.2plus-pdfsg3/zookeeper_8h_484b8767fa8f150cd15923fabafa90de.html) does indeed confirm that: "Returns: a pointer to the *opaque* zhandle structure" – kaylum Mar 17 '16 at 05:35
  • 1
    Agree with @kaylum. You can't do `zh->fd` as it is an opaque pointer. – Mohit Jain Mar 17 '16 at 05:38
  • 2
    Note: They say [you shouldn't cast the result of `malloc()` in C](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc). – MikeCAT Mar 17 '16 at 05:38
  • @kaylum Many thanks for your reply. I think that's the point. – HuangJie Mar 17 '16 at 05:44

0 Answers0