tags:

views:

59

answers:

2
typedef struct {
char name[10];
} A;
A * inst = get_instance_of_A();
char *str = g_strdup ( inst->name );

The last line doesn't compile. I also tried &(inst->name) with no luck. The error I get is:

Error: char is not a structure type.

I understand that char[] and char * are different types altogether. But shouldn't g_strdup be able to take a starting position of a C string and dupe it? If I do the following it works:

char temp[10];
strncpy(temp,inst->name,9);
char *str = g_strdup ( temp );

How can I achieve what I am trying to do without making a local char array copy? I think I am not passing the argument correctly in the fist scenario as in both cases g_strdup is being passed a char array.

A: 

use gchar instead of char in struct.

Manav MN
That shouldn't matter. It will give a warning but not an error.
HeretoLearn
`gchar` is just a synonym for `char` in GLib.
ptomato
+1  A: 

I don't think your problem lies where you think it does. gchar and char are for all intents and purposes the same. For example, this code works fine for me:

#include <glib.h>

typedef struct {
    char name[10];
} A;

A global_A = { "name here" };

A *
get_instance_of_A(void)
{
    return &global_A;
}

int
main(int argc, char **argv)
{
    const A *inst = get_instance_of_A();
    char *str = g_strdup(inst->name);
    g_print("%s\n", str);
    return 0;
}
ptomato
Indeed, "char is not a structure type" would be better explained by another problem, such as `inst` having type `char*` at the statement where `inst->name` occurs.
Pascal Cuoq