What is Struct - C Structure

C examples for Structure:Structure Definition

Introduction

A struct is a type for grouping related variables under a single name.

For example, we can group first name and last name as name struct.

To define a structure you use the struct keyword.

struct point {
  int x, y;
};

Structs may contain variables, pointers, arrays, or other user-defined types.

To declare a variable of a struct type, use the struct keyword followed by the type name and the variable identifier.

int main(void) {
  struct point p; /* object declaration */
}

We can left out the optional struct identifier, struct without an identifier is called an unnamed struct.

In this way we can prevent any more instances of the type from being created.

struct /* unnamed struct */
{
  int x, y;
} a, b; /* object declarations */

You can use typedef when defining structures.

Demo Code

typedef struct point point;

struct point {/*w w w.  java 2s.  c o  m*/
  int x, y;
};

int main(void) {
  point p; /* struct omitted */
}

Related Tutorials