Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why is it possible to use undefined struct in c

Tags:

c

#include <stdio.h>

int main()
{
  printf("%d", sizeof(struct token *));
}

The above code can be compiled and linked use gcc under Linux. Could anyone of you explain the thing behind the Scenes to me? I know the point take the fix size of memory, so the struct token is irrelevant to sizeof, but even turn on the warning option in gcc, no warnings about the "none exist" struct at all. The context for the question is that I'm reading some source code by others, I'm trying very very hard to find the definition of "struct token", but off course failed.

like image 839
Haiyuan Zhang Avatar asked May 25 '12 08:05

Haiyuan Zhang


People also ask

Why can’t we use + in C structure?

The C structure does not allow the struct data type to be treated like built-in data types: We cannot use operators like +,- etc. on Structure variables. For example, consider the following code: No Data Hiding: C Structures do not permit data hiding. Structure members can be accessed by any function, anywhere in the scope of the Structure.

What are the limitations of structures in C?

However, C structures have some limitations. The C structure does not allow the struct data type to be treated like built-in data types: We cannot use operators like +,- etc. on Structure variables. For example, consider the following code: struct number. {.

What is a structure in C++?

What is a structure? A structure is a user defined data type in C/C++. A structure creates a data type that can be used to group items of possibly different types into a single type.

Is struct an optional keyword in C++?

Note: In C++, the struct keyword is optional before in declaration of a variable. In C, it is mandatory. How to initialize structure members? Structure members cannot be initialized with declaration.


2 Answers

Because you are trying to get the size of a pointer to struct token. The size of a pointer doesn't depend on how the structure is actually defined.

Generally, you can even declare a variable of type struct token*, but you can't dereference it (e. g. access a member through the pointer).

like image 78
Alex Bakulin Avatar answered Oct 07 '22 14:10

Alex Bakulin


To paraphrase the C standard, an incomplete type is a type that describes an object but lacks information needed to determine its size.

void is another incomplete type. Unlike other incomplete types, void cannot be completed.

This "incomplete type" is often used for kinds of handle: a library allows you to allocate a "handle" to something, work with it and dispose it again. All this happens encapsulated in the library. You as user have no idea what might happen inside.

Example:

lib.h:

struct data * d_generate(void);
void d_set(struct data *, int);
int d_get(struct data *);
void d_free(struct data*);

lib.c:

#include "lib.h"
#include <stdlib.h>
struct data { int value; };
struct data * d_generate(void) {
    return malloc(sizeof(struct data))
}
void d_set(struct data * d, int v) {
    d -> value = v;
}
int d_get(struct data * d) {
    return d->value;
}
void d_free(struct data* d) {
   free(d);
}

user.c:

#include "lib.h"
[...]
struct data * d = d_generate();
d_set(d, 42);
int v = d_get(d);
// but v = d->value; doesn't work here because of the incomplete definition.
d_free(d);
like image 9
glglgl Avatar answered Oct 07 '22 13:10

glglgl