How to create an array of structs of strings and numbers and arrays in c

1 Answer

0 votes
#include <stdio.h>
#include <string.h>

#define SIZE 5

typedef struct Employee {
    char Name[16];
    int ID;
    int Week[7];
} Employees;


int main()
{
    Employees emp[SIZE] = {
        {.Name = "Tom", .ID = 89234, .Week = {1, 2, 3, 4, 5, 6, 7}},
    };

   
    emp[1].ID = 90193;
    strcpy(emp[1].Name, "Emma");
    emp[1].Week[0] = 3; 
    emp[1].Week[1] = 5; 

    for (int i = 0; i < SIZE; i++) {
        printf("Name: %s - ID: %d\n", emp[i].Name, emp[i].ID);
        printf("Week: ");
        for (int day = 0; day < 7; day++) {
            printf("%d ", emp[i].Week[day]);
        }
        printf("\n\n");
    }

    return 0;
}



/*
run:

Name: Tom - ID: 89234
Week: 1 2 3 4 5 6 7 

Name: Emma - ID: 90193
Week: 3 5 0 0 0 0 0 

Name:  - ID: 0
Week: 0 0 0 0 0 0 0 

Name:  - ID: 0
Week: 0 0 0 0 0 0 0 

Name:  - ID: 0
Week: 0 0 0 0 0 0 0 

*/

 



answered Feb 19, 2024 by avibootz
...