Namespaces
Variants
Views
Actions

c/language/struct

From cppreference.com
< c‎ | language
Revision as of 17:34, 31 May 2013 by P12bot (Talk | contribs)

Compound types are types that can hold multiple data members.

Contents

Syntax

struct name {

} instance ;

Explanation

Keywords

struct

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