What purpose does the declaration "int t[0];" serve? - c

What is the purpose of the following declaration?
struct test
{
int field1;
int field2[0];
};

That's simply a 0 length array. According to http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html:
Zero-length arrays are allowed in GNU C. They are very useful as the
last element of a structure which is really a header for a
variable-length object:
struct line {
int length;
char contents[0];
};
struct line *thisline = (struct line *)
malloc (sizeof (struct line) + this_length);
thisline->length = this_length;

It's a zero size array, which is a useful GCC extension if you don't have C99.

It's for encapsulation.
It is used to create an interface without knowing any details.
Following is a simple example.
In test.h (interface), it shows that there is a struct test_t which has two fields.
And it has three function, first one is to create the structure.
set_x is to store some integer into the structure.
get_x is to get the stored integer.
So, when can we store x?
The person who is responsible for implement (test.c) will declare another structure which contains x.
And play some tricks in "test_create" to malloc this structure.
Once the interface and the implement have been done.
The application (main.c) can set/get x without knowing where it is.
test.h
struct test_t
{
int field1;
int field2[0];
};
struct test_t *test_create();
void set_x(struct test_t *thiz, int x);
int get_x(struct test_t *thiz);
test.c
#include "test.h"
struct test_priv_t {
int x;
};
struct test_t *test_create()
{
return (struct test_t*)malloc(sizeof(struct test_t) + sizeof(struct test_priv_t);
}
void set_x(struct test_t *thiz, int x)
{
struct test_priv_t *priv = (struct test_priv_t *)thiz->field2;
}
int get_x(struct test_t *thiz)
{
struct test_priv_t *priv = (struct test_priv_t *)thiz->field2;
}
main.c
#include "test.h"
int main()
{
struct test_t *test = test_create();
set_x(test, 1);
printf("%d\n", get_x(test));
}

Related

struct in c with an attribute that is a pointer to the same struct

i need to create a struct with an attribute that is a pointer to the same struct.
i'm trying this solution but not work:
typedef struct
{
int number;
void *other;
}mystruct;
extern mystruct first[];
extern mystruct second[];
mystruct first[] = {{1,NULL},{2,second}};
mystruct second[] = {{3,NULL},{4,first}};
mystruct *wrap;
wrap = (mystruct *)first[1].other;
int main(void){
printf("%d\n",first[0].number);
printf("%d\n",second[0].number);
printf("%d\n",wrap[1].number);
}
can someone help me?
best regards and thankyou
In C, you can name the struct before using it and typdefing it:
typedef struct mystruct_
{
int number;
struct mystruct_ *other;
} mystruct
I'm not entirely sure but are you looking for some sort of linked-lists or precisely speak Self Referential structure
struct list {
int something;
struct list *use_this_to_point_to_similar_type;
};
Here is another good reference what-is-self-referencing-structure-in-c
just a little bit simplification, and moving few instructions here and there, below code is a loosely written example of possibly what you are looking forward to achieve
#include<stdio.h>
struct mystruct
{
int number;
struct mystruct *other;
};
struct mystruct first[] = {{1,NULL},{2,NULL}};
struct mystruct second[] = {{3,NULL},{4,NULL}};
struct mystruct *wrap;
int main(void)
{
first[1].other = second;
second[1].other = first;
wrap = first[1].other;
printf("%d\n",first[0].number);
printf("%d\n",second[0].number);
printf("%d\n",wrap[1].number);
return 0;
}
your first and second don't need to be extern as they are allocated within your program. you can declare and init. var prior to the main. but the rest you must move into the main function:
int main(void){
wrap = (first[1].other);
printf("%d\n",first[0].number);
printf("%d\n",first[1].number);
printf("%d\n",second[0].number);
printf("%d\n",wrap[1].number);
return 0;}

C - compiler error: dereferencing pointer to incomplete type

I've seen many questions here about dereferencing pointers to incomplete types but every single one of them is related to not using typedef or to having the structs declared in the .c, not in the header file. I've been trying to fix this for many hours and can't seem to find a way.
stable.h (cannot be changed):
typedef struct stable_s *SymbolTable;
typedef union {
int i;
char *str;
void *p;
} EntryData;
SymbolTable stable_create();
stable.c:
SymbolTable stable_create() {
SymbolTable ht = malloc(sizeof (SymbolTable));
ht->data = malloc(primes[0] * sizeof(Node));
for (int h = 0; h < primes[0]; h++) ht->data[h] = NULL;
ht->n = 0;
ht->prIndex = 0;
return ht;
}
aux.h:
#include "stable.h"
typedef struct {
EntryData *data;
char *str;
void *nxt;
} Node;
typedef struct {
Node **data;
int n;
int prIndex;
} stable_s;
typedef struct {
char **str;
int *val;
int index;
int maxLen;
} answer;
freq.c:
answer *final;
static void init(SymbolTable table){
final = malloc(sizeof(answer));
final->val = malloc(table->n * sizeof(int));
}
int main(int argc, char *argv[]) {
SymbolTable st = stable_create();
init(st);
}
compiler error (using flags -Wall -std=c99 -pedantic -O2 -Wextra):
freq.c:13:30: error: dereferencing pointer to incomplete type ‘struct stable_s’
final->val = malloc(table->n * sizeof(int));
This code
typedef struct stable_s *SymbolTable;
defines the type SymbolTable as a pointer to struct stable_s.
This code
typedef struct {
Node **data;
int n;
int prIndex;
} stable_s;
defines a structure of type stable_s. Note that stable_s is not struct stable_s.
A simple
struct stable_s {
Node **data;
int n;
int prIndex;
};
without the typedef will solve your problem.
See C : typedef struct name {...}; VS typedef struct{...} name;
As Andrew points out, declaring a "struct stable_s { ... }" will make things compile.
However, you don't say whether this is a class assignment or real-world. If real-world, it's probably an extremely bad idea to declare the struct yourself. You are being given an opaque type to use to reference a library; you are not supposed to know or access the stuff inside. The library is relying on various semantics that you might mess up, and as software versions change, the contents of the struct can (and almost certainly will) change, so your code will break in the future.

sizeof typedef pointer

I have a struct that defined like that:
typedef struct my_struct
{
int numbers[10];
}
*my_struct;
Is there a way to find out its size?
sizeof(my_struct);// return size of a pointer
The struct type itself is spelled with struct, so you can say:
sizeof (struct my_struct)
This would not work if you hadn't also given your struct a name, which would have been possible:
typedef struct { int numbers[10]; } * foo; /* struct type has no name */
foo p = malloc(1000);
p->numbers[3] = 81;
I'd say all of this is poor code that is needlessly terse for no reason. I would just keep all the names unique, and name everything, and not alias pointers, for that matter. For example:
typedef struct my_struct_s my_struct;
my_struct * create_my_struct(void);
void destroy_my_struct(my_struct * p);
struct my_struct_s
{
int numbers[10];
};
Everything has a unique name, the typedef is separate from the struct definition, and pointers are explicit.

Nested linked lists in C

I'm trying to implement a nested linked list in C, that will be used for a hierarchical menu. However, the GCC (v4.9.3-1) is complaining to nested structures, and I have no idea how to fix this. Here is the minimum (non)working example.
Is this nesting even possible in C?
main.c
#include "menu.h"
int main(void) {
Init_Menu();
return 0;
}
menu.c
#include "menu.h"
MenuItem_t LVL_0_MainMenu = {
.size = 0,
};
MenuItem_t LVL_1_Measurements = {
.size = 0,
};
void Init_Menu(void) {
Menu_Add_Child(&LVL_0_MainMenu, &LVL_1_Measurements);
}
void Menu_Add_Child(MenuItem_t *parent, MenuItem_t *child) {
parent->children[parent->size] = child;
child->parent = parent;
parent->size++;
}
menu.h
typedef struct {
unsigned char size;
MenuItem_t children[10];
MenuItem_t *parent;
} MenuItem_t;
extern MenuItem_t LVL_0_MainMenu;
extern MenuItem_t LVL_1_Measurements;
void Init_Menu(void);
void Menu_Add_Child(MenuItem_t *parent, MenuItem_t *child);
Based on answers by #bolov and #sps (once again, thanks to both of them), here is the minimum working example:
main.c
#include "menu.h"
int main(void) {
Init_Menu();
return 0;
}
menu.c
#include "menu.h"
MenuItem_t LVL_0_MainMenu = {
.size = 0,
};
MenuItem_t LVL_1_Measurements = {
.size = 0,
};
void Init_Menu(void) {
Menu_Add_Child(&LVL_0_MainMenu, &LVL_1_Measurements);
}
void Menu_Add_Child(MenuItem_t *parent, MenuItem_t *child) {
parent->children[parent->size] = child;
child->parent = parent;
parent->size++;
}
menu.h
struct MenuItem_t {
unsigned char size;
struct MenuItem_t *children[10];
struct MenuItem_t *parent;
};
typedef struct MenuItem_t MenuItem_t;
extern MenuItem_t LVL_0_MainMenu;
extern MenuItem_t LVL_1_Measurements;
void Init_Menu(void);
void Menu_Add_Child(MenuItem_t *parent, MenuItem_t *child);
The difference between this corrected program and the original (non)working program, is that the children array is defined as an array of pointers to variables of the type MenuItem_t instead of the array of variables of the same type. The other difference is that a nested list (inside the structure) should also contain the keyword struct as #bolov explained.
You need to use struct for the type used inside itself, even if you typedef it later on.
E.g. this won't work:
struct X_ {
X* next;
};
typedef struct X_ X;
But this will
struct X_ {
struct X_* next;
};
As a side note, I really don't like this form:
typedef struct {
} X;
I use:
struct X {
};
typedef struct X X;
But maybe this is just me being more fond of C++.
If you want to use that form, it's the same: you need to add struct and it works:
typedef struct {
struct X2* next;
} X2;
regarding:
struct X {
struct X arr[10];
};
You can't have that! The array is just in our way to understand why. So let's simplify:
struct X {
int a;
struct X var;
};
This can't be. What size would X be? sizeof(X) = sizeof(int) + sizeof(X) + padding. Do you see the problem? All you can do is have a pointer to X, but not an object X inside X.
Returning to your array. You need dynamic arrays:
struct X {
struct X* arr;
int arr_size;
};
It gets more complicated as you need to manage the memory (malloc/free fun), but you can't avoid it.
First of all, you cannot do,
typedef struct {
SomeName_t some_var;
} SomeName_t;
You need to do,
typedef struct somename {
struct somename some_var;
} SomeName_t;
Also, a struct cannot have a member which is an array of structure itself. However, a struct can have a member which is an array of pointer to the same structure.
struct foo {
struct foo foo_arr[10]; /* Will give error */
struct foo *foo_ptr_arr[10]; /* Legal */
};
However, I dont see a reason that your children member should be an array of struct anyways. Because, as can be seen in menu.c, you are doing
parent->children[parent->size] = child;
where the type of child is MenuItem_t *. So I think you basically wanted MenuItem_t.children to be an array of MenuItem_t *, and not an array of MenuItem_t.
So making this change should resolve your issue:
menu.h
typedef struct menuitem {
unsigned char size;
/* MenuItem_t children[10]; */ /* Not possible */
struct menuitem *children[10]; /* This is what you want to do */
struct menutem *parent;
} MenuItem_t;

error: dereferencing pointer to incomplete type

Looked through many other SO posts related to this, but none were able to help me. So, I have the following structs defined:
typedef struct
{
int created;
double data;
int timeLeft;
int destination;
}dataPacket;
typedef struct
{
dataPacket *array;
int currIndex;
int firstIndex;
int nextTick;
int maxLength;
int length;
int stime;
int total;
}packetBuffer;
typedef struct{
int mac;
struct wire *lconnection;
struct wire *rconnection;
int numRecieved;
struct packetBuffer *buffer;
int i;
int backoff;
}node;
typedef struct{
float length;
float speed;
int busy;
struct dataPacket *currPacket;
struct node *lnode;
struct node *rnode;
}wire;
And then I'm trying to use the following function:
int sendPacket(node *n, int tick)
{
if(n->buffer->length > 0)
{
if(n->backoff <= 0)
{
if (n->lconnection->busy != 0 || n->lconnection->busy != 0)
{
n->i++;
n->backoff = (512/W * genrand()*(pow(2,n->i)-1))/TICK_LENGTH;
}
else
{
n->lconnection->busy = 1;
n->rconnection->busy = 1;
n->lconnection->currPacket = n->buffer[n->buffer->currIndex];
n->rconnection->currPacket = n->buffer[n->buffer->currIndex];
}
}
else
{
n->backoff--;
}
}
}
I'm getting the error described in the title everytime I try to access a member of buffer, lconnection, or rconnection.
struct packetBuffer *buffer;
You've defined a type packetBuffer (a typedef for an otherwise anonymous struct).
You haven't defined struct packetBuffer.
In the absence of an existing type struct packetBuffer, the compiler treats it as an incomplete type, assuming that you'll complete it later. The declaration
struct packetBuffer *buffer;
is perfectly legal, but you can't dereference buffer unless the type struct packetBuffer is visible.
Just drop the struct keyword.
(My personal preference is to drop the typedef and consistently refer to struct types as struct whatever, but that's a matter of style and taste.)
The following:
typedef struct {
int x;
char *y;
...
} my_struct;
creates an identifier for an anonymous structure. In order, for a structure to refer to an instance of itself, it must not be "anonymous":
typedef struct my_struct {
int x;
char *y;
struct my_struct *link
....
} my_struct_t;
This means that my_struct_t is now the type struct my_struct and not just an anonymous struct. Also, note that struct my_struct can be used within its own structure definition. That is not possible with anonymous structs.
As a final complication, the my_struct in struct my_struct is in a differenct "namespace" than the my_struct_t. This is sometimes used to to simplify (or confuse) things in code like this:
typedef struct my_struct {
int x;
char *y;
struct my_struct *link
....
} my_struct;
Now I can use my_struct anywhere in my code instead of struct my_struct.
Finally, you could separate the typedef from the structure definition to achieve the same effect:
struct my_struct {
int x;
char *y;
struct my_struct *link;
....
};
typedef struct my_struct my_struct;
As noted in David R.Hanson's C Interfaces and Implementations, "This definition is legal because structure, union, and enumeration tags occupy a same name space that is separate from the space for variables, functions, and type names."

Resources