Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,900 questions

51,831 answers

573 users

How to initialize array in 2D array of structs with zeros and multiple numbers at specific indexes in C

1 Answer

0 votes
#include <stdio.h>

typedef struct {
    int arr[4];
    char ch;
} ST;

int main(void) {

    static const ST s[2][3] = { { {{7,8}, 'z'}, {{9}, 'a'}, {{4,2,3}, 'w'} }, { {{88,99}, 'm'} } };

    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 3; j++) {
            for (int k = 0; k < 4; k++) {
                printf("s[%d][%d].arr[%d] = %d\n", i, j, k, s[i][j].arr[k]);
            }
            printf("s[%d][%d].ch = %c\n\n", i, j, s[i][j].ch);
        }
    }

    return 0;
}





/*
run:

s[0][0].arr[0] = 7
s[0][0].arr[1] = 8
s[0][0].arr[2] = 0
s[0][0].arr[3] = 0
s[0][0].ch = z

s[0][1].arr[0] = 9
s[0][1].arr[1] = 0
s[0][1].arr[2] = 0
s[0][1].arr[3] = 0
s[0][1].ch = a

s[0][2].arr[0] = 4
s[0][2].arr[1] = 2
s[0][2].arr[2] = 3
s[0][2].arr[3] = 0
s[0][2].ch = w

s[1][0].arr[0] = 88
s[1][0].arr[1] = 99
s[1][0].arr[2] = 0
s[1][0].arr[3] = 0
s[1][0].ch = m

s[1][1].arr[0] = 0
s[1][1].arr[1] = 0
s[1][1].arr[2] = 0
s[1][1].arr[3] = 0
s[1][1].ch =

s[1][2].arr[0] = 0
s[1][2].arr[1] = 0
s[1][2].arr[2] = 0
s[1][2].arr[3] = 0
s[1][2].ch =

*/

 



answered Apr 24, 2023 by avibootz
...