Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and work.


Instant Grammar Checker - Correct all grammar errors and enhance your writing

You have the vaccine, now let everyone know!
Teach Your Child To Read

BlueHost Web Hosting Black Friday

Bonsai Boy of New York Bonsai Trees and Accessories

DreamCloud The Comfortable Luxury Mattress Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting

Disclosure We are a professional Programming & Software Q&A website, that receives compensation from some of the links whose we show you with Information. It means that my content may contain affiliate links.

17,954 questions

24,129 answers

573 users

How to initialize array of structs in C

1 Answer

0 votes
#include <stdio.h>

typedef struct User {
    char firstname[30];
    char lastname[30];
    int age;
} User;

int printUser(User *p) {
    if (p == NULL)
        return -1;

    printf("Name:%s Last Name:%s Age:%d\n", p->firstname, p->lastname, p->age);

    return 0;

int main(void) {
    User arr[] = {
            { "Arthur", "Nimbus", 43},
            { "Alex", "Blade", 52},

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

    for (int i = 0; i < size; ++i) {



Name:Arthur Last Name:Nimbus Age:43
Name:Alex Last Name:Blade Age:52



answered May 4 by avibootz