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,870 questions

51,793 answers

573 users

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

3 Answers

0 votes
#include <stdio.h>

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

int main(void) {

    static const ST s[2] = { {{ 8, 9 }, 'z'}, {{3}, 'a'} };

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

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

    return 0;
}




/*
run:

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

*/


 



answered Apr 24, 2023 by avibootz
0 votes
#include <stdio.h>

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

int main(void) {

    static const ST s[] = { {{ 3, 5, 9 }, 'z'}, {{7}, 'a'}, {{ 4, 8 }, 'x'} };

    size_t size = sizeof s / sizeof s[0];

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

    return 0;
}




/*
run:

s[0].arr[0] = 3
s[0].arr[1] = 5
s[0].arr[2] = 9
s[0].arr[3] = 0
s[0].arr[4] = 0
s[0].ch = z
s[1].arr[0] = 7
s[1].arr[1] = 0
s[1].arr[2] = 0
s[1].arr[3] = 0
s[1].arr[4] = 0
s[1].ch = a
s[2].arr[0] = 4
s[2].arr[1] = 8
s[2].arr[2] = 0
s[2].arr[3] = 0
s[2].arr[4] = 0
s[2].ch = x

*/

 



answered Apr 24, 2023 by avibootz
edited Apr 24, 2023 by avibootz
0 votes
#include <stdio.h>

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

int main(void) {

    static const ST s[] = { {{ 3, 5, 9 }, 'z'}, {{7}, 'a'}, {{ 4, 8 }, 'x'} };

    size_t struct_size = sizeof s / sizeof s[0];
    size_t struct_array_size = sizeof s[0].arr / sizeof(int);

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

    return 0;
}




/*
run:

s[0].arr[0] = 3
s[0].arr[1] = 5
s[0].arr[2] = 9
s[0].arr[3] = 0
s[0].arr[4] = 0
s[0].ch = z
s[1].arr[0] = 7
s[1].arr[1] = 0
s[1].arr[2] = 0
s[1].arr[3] = 0
s[1].arr[4] = 0
s[1].ch = a
s[2].arr[0] = 4
s[2].arr[1] = 8
s[2].arr[2] = 0
s[2].arr[3] = 0
s[2].arr[4] = 0
s[2].ch = x

*/


 



answered Apr 24, 2023 by avibootz
...