jewfro
jewfro

Reputation: 253

json-c parsing - error dereferencing pointer to incomplete type

I've been trying to use the following code, taken from an example, I've had to change json_object_object_get(struct json_object *obj, const char *key) to json_object_get_ex(struct json_object *obj, const char *key, struct json_object **value)

I'm sorry I've already posted similar questions as I've been trying to find a way to parse json from a socket for days and I'm getting desperate, but I've done some more work and research and I think this is much close. The error I'm getting from compiling the below is:

server4.c: In function ‘main’: server4.c:62: error: dereferencing pointer to incomplete type server4.c:68: warning: assignment makes pointer from integer without a cast

    struct json_object *jobj, *val_jobj, *value;                                        
    char const *val;                                                              
    char buf[50];                                                                              
    fgets(buf, sizeof(buf), stdin);                                               
    printf("Input JSON : %s", buf);         
    char const *val;
    *jobj = json_tokener_parse(buf);
    if (is_error(jobj))    
    return (-1);  
    printf("Received JSON in String format : %s\n",    
    json_object_to_json_string(jobj));

  //Get the value for the key "name"      
    val_jobj = json_object_object_get_ex(jobj, "name", &value);    
    printf("Extracted value for command : %s\n",    

    //Get the string from the value of the key "name"                                         
    val = json_object_get_string(val_jobj);                                       
    printf("String value returned : %s\n", val);      

I can't see what is wrong and I don't fully understand how json-c works, I'm also more familiar with c++, though of course I use pointers there too. Either way, reading through some json parsers for c++, I've found them way easier to understand.

Thanks in advance

Upvotes: 1

Views: 1014

Answers (2)

kaylum
kaylum

Reputation: 14046

The json_object structure is an opaque type. It is private and pointers to it can not be dereferenced by code outside the json-c library.

json_tokener_parse returns json_object* so line 62 should be:

jobj = json_tokener_parse(buf);

That is, don't dereference jobj.

Upvotes: 1

mlp
mlp

Reputation: 825

The only dereference shown in your code is

*jobj = json_tokener_parse(buf);

so this must be the line 62 to which the error message refers.

You're telling the compiler to take the value of the pointer jobj (to which you never assigned a value, so there's error number 1) and in the space pointed-to by that value to store the value returned from json_tokener_parse(buf). Trouble is, you've not told the compiler what a struct json_object is, so jobj is a pointer to a type which is incomplete - the compiler doesn't know how big it is, nor what components it's made of. I presume there's a JSON header file that you've neglected to #include.

The warning about line 68 is a separate but likely related problem, but the line which would be 68 (if my guess about 62 is correct) is a comment.

Upvotes: 0

Related Questions