If I have a struct in C that has an integer and an array, how do I initialize the integer to 0 and the first element of the array to 0, if the struct is a member another struct so that for every instance of the other struct the integer and the array has those initialized values?
-
4code example illustrating your problem would be better ... – Agnius Vasiliauskas Oct 03 '12 at 06:11
-
Good Read: [C and C++ : Partial initialization of automatic structure](http://stackoverflow.com/questions/10828294/c-and-c-partial-initialization-of-automatic-structure) – Alok Save Oct 03 '12 at 06:21
5 Answers
Initialisers can be nested for nested structs, e.g.
typedef struct {
int j;
} Foo;
typedef struct {
int i;
Foo f;
} Bar;
Bar b = { 0, { 0 } };

- 208,748
- 37
- 389
- 560
I hope this sample program helps....
#include <stdio.h>
typedef struct
{
int a;
int b[10];
}xx;
typedef struct
{
xx x1;
char b;
}yy;
int main()
{
yy zz = {{0, {1,2,3}}, 'A'};
printf("\n %d %d %d %c\n", zz.x1.a, zz.x1.b[0], zz.x1.b[1], zz.b);
return 0;
}
yy zz = {{0, {0}}, 'A'};
will initialize all the elements of array b[10] will be set to 0.
Like @unwind suggestion, In C all instances created should initialized manually. No constructor kind of mechanism here.

- 9,158
- 7
- 41
- 63
-
1You can still use constructors/destructors in C, the downside is that you will have to call them explicitly, they won't be called automatically as they would in a language with built-in OO support. See my answer for an example. – Lundin Oct 03 '12 at 08:28
-
1@Lundin Thanks. But mystruct_construct() needs to be called for every instance(s) right??? – Jeyaram Oct 03 '12 at 09:00
You can 0-initialize the whole struct with {0}.
For example:
typedef struct {
char myStr[5];
} Foo;
typedef struct {
Foo f;
} Bar;
Bar b = {0}; // this line initializes all members of b to 0, including all characters in myStr.
-
1
-
1you mean b.f = {0} or b.f = someOtherF? Otherwise there are no constructor in C so you can't implement a default initialization for f. – yuklai Oct 03 '12 at 06:53
C doesn't have constructors, so unless you are using an initializer expression in every case, i.e. write something like
my_big_struct = { { 0, 0 } };
to initialize the inner structure, you're going to have to add a function and make sure it's called in all cases where the structure is "instantiated":
my_big_struct a;
init_inner_struct(&a.inner_struct);

- 391,730
- 64
- 469
- 606
-
1It is safer and more elegant to do this with "opaque type" (aka incomplete type). Then the caller won't have access to the private member variables of the struct. – Lundin Oct 03 '12 at 08:31
Here is an alternative example how you would do things like this with object-oriented design. Please note that this example uses runtime initialization.
mystruct.h
#ifndef MYSTRUCT_H
#define MYSTRUCT_H
typedef struct mystruct_t mystruct_t; // "opaque" type
const mystruct_t* mystruct_construct (void);
void mystruct_print (const mystruct_t* my);
void mystruct_destruct (const mystruct_t* my);
#endif
mystruct.c
#include "mystruct.h"
#include <stdlib.h>
#include <stdio.h>
struct mystruct_t // implementation of opaque type
{
int x; // private variable
int y; // private variable
};
const mystruct_t* mystruct_construct (void)
{
mystruct_t* my = malloc(sizeof(mystruct_t));
if(my == NULL)
{
; // error handling needs to be implemented
}
my->x = 1;
my->y = 2;
return my;
}
void mystruct_print (const mystruct_t* my)
{
printf("%d %d\n", my->x, my->y);
}
void mystruct_destruct (const mystruct_t* my)
{
free( (void*)my );
}
main.c
int main (void)
{
const mystruct_t* x = mystruct_construct();
mystruct_print(x);
mystruct_destruct(x);
return 0;
}
You don't necessarily need to use malloc, you can use a private, statically allocated memory pool as well.

- 195,001
- 40
- 254
- 396