Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize an array variable inside a structure?

Tags:

c

struct

I want to initialize a couple of arrays which are members of a struct which is passed to a function by reference. Appreciate any help.

typedef struct Snake_pos
{
    char field[10][10];
    int Y[10];
    int X[10];
}snake_pos;

int main()
{
    snake_pos pos1;
    pos_init(&pos1);
    return 0;
}

void pos_init(snake_pos *pos1)
{
    pos1->X={};
    pos1->Y={};
    pos1->field={};
}
like image 792
manish ma Avatar asked Jun 19 '16 19:06

manish ma


People also ask

How do you initialize an array within a struct?

Here is how initialize the structure : struct str { char arr1[4]; // this string is limited to three characters plus null char arr2[4]; int num1[4]; int num2[4]; }; struct str select = { "Sam", "Joe", { 0, 1, 2, 3 }, { 4, 5, 6, 7 } };

Can you place an array inside a structure?

A structure may contain elements of different data types – int, char, float, double, etc. It may also contain an array as its member. Such an array is called an array within a structure. An array within a structure is a member of the structure and can be accessed just as we access other elements of the structure.

How do you initialize an array of variables?

You initialize an array variable by including an array literal in a New clause and specifying the initial values of the array. You can either specify the type or allow it to be inferred from the values in the array literal.

Can we initialize array with variable in C?

The C99 standard allows variable sized arrays (see this). But, unlike the normal arrays, variable sized arrays cannot be initialized.


2 Answers

You can only use the syntax = {} when defining the variable. So to zero every member you can either define it as

snake_pos pos1 = { 0 };

Or if it is passed to a function, like this

void pos_init(snake_pos *pos1)
{
    memset(pos1, 0, sizeof *pos1);
}
like image 120
Weather Vane Avatar answered Oct 23 '22 08:10

Weather Vane


You can access any array element inside of an array through a structure pointer this way :

  • for 1 D array : structure_pointer->array_valiable[index1]
  • for 2 D array : structure_pointer->array_valiable[index1][index2]

so now, you can initialize each member of arrays X , Y and field of structure snake_pos using scanf() this way :

#include <stdio.h>

typedef struct Snake_pos
{
    char field[10][10];
    int Y[10];
    int X[10];

}snake_pos;

void pos_init(snake_pos *pos1)
{
    int i,j;

    //initializing each member

    //of array X
    for(i=0;i<10;i++)
        scanf("%d",&pos1->X[i]);

    //of array Y
    for(i=0;i<10;i++)
        scanf("%d",&pos1->Y[i]);

    //of the two dimensional array field
    for(i=0;i<10;i++)
        for(j=0;j<10;j++)
            scanf(" %c",&pos1->field[i][j]); 
    //notice the space before %c is to consume white spaces returning from before scanf's
}


int main()
{


      snake_pos pos1;
      pos_init(&pos1);

  return 0;
}

Note : if you want to initialize all members to a single fixed value, then you can instead of using scanf() in for loop, assign the value this way :

  for(i=0;i<10;i++) //initializes all members with a value of 0
      pos1->X[i]=0;
like image 24
Cherubim Avatar answered Oct 23 '22 09:10

Cherubim