Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

struct — structure type

The struct keyword defines or names a structure type that groups named members, potentially of different types.

Version: C23; repository compiler flag, production runtime not attested

Syntax

struct Tag { Type member; /* ... */ };
struct Tag object;
object.member;
pointer->member;

struct groups named members into one type. End the definition struct Point { int x; int y; }; with a semicolon. Point is a tag; without a corresponding typedef, the type name is struct Point, not Point alone.

Use . for an object's member and -> through a pointer. The initializer .x = 2 names a member. Structure assignment copies member values; pointer members do not trigger deep copying.

Unlike union, members occupy separate storage. Padding can increase sizeof, so do not assume that a structure's size equals the sum of its members' sizes.

Examples

#include <stdio.h>

struct Point {
    int x;
    int y;
};

int main(void)
{
    struct Point p = {.x = 2, .y = 3};
    struct Point copy = p;
    struct Point *ptr = &copy;
    ptr->x = 5;
    printf("%d %d\n", p.x, copy.x);
    return 0;
}

Result

2 5

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources