c/language/struct

From cppreference.com

Compound types are types that can hold multiple data members.

Contents

[edit] Syntax

struct name {

} instance ;

[edit] Explanation

[edit] Keywords

struct

[edit] Example

#include <stdio.h>
 
struct car {
	char *make;
	char *model;
	int year;
};
 
int main() 
{
	/* external definition */
	struct car c;
	c.make = "Nash";
	c.model = "48 Sports Touring Car";
	c.year = 1923;
	printf("%d %s %s\n", c.year, c.make, c.model);
 
	/* internal definition */
	struct spaceship {
		char *make;
		char *model;
		char *year;
	} s;
	s.make = "Incom Corporation";
	s.model = "T-65 X-wing starfighter";
	s.year = "128 ABY";
	printf("%s %s %s\n", s.year, s.make, s.model);
 
	return 0;
}

Output:

1923 Nash 48 Sports Touring Car
128 ABY Incom Corporation T-65 X-wing starfighter