Thursday, January 30, 2025
HomeProgrammingHow Do You Make an Array of Structs in C?

How Do You Make an Array of Structs in C?

In C, you can create an array of structs by first defining the struct and then declaring an array of that struct type. Here’s an example:

c

#include <stdio.h>

struct Person {
char name[50];
int age;
};

See also  Find IP Address in Linux

int main() {
// Declare an array of structs
struct Person people[3];

// Assign values to the array elements
people[0].age = 25;
strcpy(people[0].name, “Alice”);

people[1].age = 30;
strcpy(people[1].name, “Bob”);

See also  What is the Difference between Public, Protected, Package Private, and Private in Java

// Access the elements
printf(“%s is %d years old.\n”, people[0].name, people[0].age);

return 0;
}

In this example, people is an array of 3 Person structs.

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x