LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   error: deferencing pointer to incomplete type (https://www.linuxquestions.org/questions/programming-9/error-deferencing-pointer-to-incomplete-type-681641/)

shifter 11-06-2008 05:26 PM

error: deferencing pointer to incomplete type
 
My prompt terminal give me:

Quote:

error: deferencing pointer to incomplete type
My code is:


Quote:

typedef struct _SNotebook SNotebook;

struct SNotebook {
GtkNotebook *notebook;
GtkEntry *entry;
GtkTextBuffer *buffer;
gchar contents[4];
guint page;
};

.......
.......
.......

void callback_run(GtkButton *button, SNotebook *state) {

.......
.......
.......

child = gtk_notebook_get_nth_page(state->notebook, state->page);

if (strcmp(gtk_notebook_get_tab_label_text(&state->notebook, child), "Search") == 0) g_print("pkg_search\n");
if (strcmp(gtk_notebook_get_tab_label_text(&state->notebook, child), " Add ") == 0) g_print("pkg_add\n");
if (strcmp(gtk_notebook_get_tab_label_text(&state->notebook, child), " Info ") == 0) g_print("pkg_info\n");
if (strcmp(gtk_notebook_get_tab_label_text(&state->notebook, child), "Delete") == 0) g_print("pkg_delete\n");


}


.......
.......
.......

int main(int argc, char* argv[]) {

.......
.......
.......

g_signal_connect(G_OBJECT(run_button), "clicked", G_CALLBACK(callback_run), &state);

}
The error:

error: deferencing pointer to incomplete type

is referred at state pointer, what is error?

Thanks

chrism01 11-06-2008 05:37 PM

Its a while since I did C, but I'm pretty sure you have to define the struct before you can reference it in a typedef (or anything else).

raconteur 11-06-2008 05:42 PM

Quote:

Originally Posted by chrism01 (Post 3333930)
Its a while since I did C, but I'm pretty sure you have to define the struct before you can reference it in a typedef (or anything else).

Yep, agreed. You can do both in one swell foop as well:

Code:

typedef struct _SNotebook {
GtkNotebook *notebook;
GtkEntry *entry;
GtkTextBuffer *buffer;
gchar contents[4];
guint page;
} SNotebook;

I am assuming that you declare and allocate this somewhere as well... defining a type doesn't prepare it for use, obviously, but I'm sure you've covered this elsewhere in your code.

Quakeboy02 11-06-2008 05:45 PM

It's been a long time for me, too, but you didn't tell us specifically which statement was giving the error, so I'm going to consider this as a test. This type of thing "&state->notebook" always caused me problems. I almost always had to rewrite it as either "&(state->notebook)" or even something like "(GtkNotebook *) &(state->notebook)". Like I said, it's been a long time.

chrism01 11-06-2008 06:25 PM

@raconteur: indeed, but I've always preferred to keep 'em separate, especially if there's a danger needing more than one instance.
To me it's more K.I.S.S
:)

raconteur 11-07-2008 01:13 PM

Quote:

Originally Posted by chrism01 (Post 3333962)
@raconteur: indeed, but I've always preferred to keep 'em separate, especially if there's a danger needing more than one instance. To me it's more K.I.S.S :)

I certainly don't want to derail this thread, if the OP still has problems, please let us know...

That being said, I'm not quite sure I understand the above quote.
[edit -- maybe I do, please see the last paragraph]

Once a data type is defined, multiple instances are simple matter of declaration (and allocation if necessary), just like any other data type.

For instance (no pun intended):
Code:

typedef struct _SNotebook {
GtkNotebook *notebook;
GtkEntry *entry;
GtkTextBuffer *buffer;
gchar contents[4];
guint page;
} SNotebook;

[...]
SNotebook state1;
SNotebook state2;
SNotebook* state3 = (SNotebook *) malloc(sizeof(SNotebook));
[...]

To the OP, please forgive me if this is obvious to you, but I couldn't tell from your question or code just how familiar you are with the C preprocessor and C language data type definition and declaration methods, so I'll add this:

The _Snotebook tag in the above definition is unnecessary in this case.
The only time you need that type of tag is when you want to self-reference the type within the type itself (such as when you wish to create linked lists). Of course, having that tag there does no harm at all if it isn't used. The only odd thing you may see is that symbolic debuggers will usually display the type with the tag name.

Here is an example of a self-referencing data type:
Code:

typedef struct _SNotebook {
GtkNotebook *notebook;
GtkEntry *entry;
GtkTextBuffer *buffer;
gchar contents[4];
guint page;
_SNotebook* pPrevNotebook;
_SNotebook* PNextNotebook;
} SNotebook;

That sort of construct would not be possible without the tag.

However, in your case, with no self-referencing members in the struct, this definition would do just as well:
Code:

typedef struct {
GtkNotebook *notebook;
GtkEntry *entry;
GtkTextBuffer *buffer;
gchar contents[4];
guint page;
} SNotebook;

The same declaration and allocation caveats apply to both methods, of course.

If we really wanted to confuse things, we could point out that that things rather fundamentally change when the struct is only declared instead of being defined as a type. I won't go into all of that, K&R have done it very well.

It occurs to me that those differences may be what chrism01 was saying. When multiple instances of a declared-only data structure are required, things can get verbose pretty quickly.


All times are GMT -5. The time now is 04:26 AM.