4

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?

Nathan Yeung
  • 147
  • 1
  • 9

5 Answers5

7

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 } };
Paul R
  • 208,748
  • 37
  • 389
  • 560
3

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.

Jeyaram
  • 9,158
  • 7
  • 41
  • 63
  • 1
    You 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
1

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.
Marlon
  • 19,924
  • 12
  • 70
  • 101
yuklai
  • 1,595
  • 1
  • 14
  • 26
1

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);
unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    It 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
1

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.

Lundin
  • 195,001
  • 40
  • 254
  • 396