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.

40,026 questions

51,982 answers

573 users

How to initialize struct with multiple array in C

1 Answer

0 votes
#include <stdio.h>
  
typedef struct {
    unsigned char array1[4];
    unsigned int  array2[3];
    unsigned char array3[5];
} ST;
  
int main(void) {
    static const ST s = { 
        { 87, 45, 13, 'a' },
        { 4, 60, 999 },
        { 'a', 'k', 'c', 'e', 'p' },
    };
  
    for (int i = 0; i < 4; i++)
        printf("s.array1[%d] = %d\n", i, s.array1[i]);
        
    printf("\n");
    
    for (int i = 0; i < 3; i++)
        printf("s.array2[%d] = %d\n", i, s.array2[i]);
        
    printf("\n");
    
    for (int i = 0; i < 5; i++)
        printf("s.array2[%d] = %c\n", i, s.array3[i]);
  
    return 0;
}
  
  
  
  
/*
run:
  
s.array1[0] = 87
s.array1[1] = 45
s.array1[2] = 13
s.array1[3] = 97

s.array2[0] = 4
s.array2[1] = 60
s.array2[2] = 999

s.array2[0] = a
s.array2[1] = k
s.array2[2] = c
s.array2[3] = e
s.array2[4] = p

*/

 



answered Oct 24, 2023 by avibootz
edited Oct 24, 2023 by avibootz

Related questions

1 answer 112 views
1 answer 93 views
1 answer 124 views
3 answers 303 views
303 views asked Dec 5, 2020 by avibootz
...