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?

2

5 Answers 5

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 } };
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

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 Thanks. But mystruct_construct() needs to be called for every instance(s) right???
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.

2 Comments

Is there a way to initialize Foo inside Bar?
you 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.
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);

1 Comment

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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.