C Programs Tutorials | IT Developer
IT Developer

C Programming - C Structures



Share with a Friend

C Programming - C Structures

C Structures

A structure in C is a user-defined data type that allows grouping variables of different types under a single name. It provides a convenient way of handling related data as a single entity and is widely used in programming for defining complex data models.

Defining a Structure

The struct keyword is used to define a structure in C.

Syntax:

C

struct StructureName {

    dataType member1;

    dataType member2;

    // Additional members

};

Example:

C

struct Point {

    int x;

    int y;

};

Declaring Structure Variables

Method 1: Separate Declaration

C

struct Point p1, p2;

Method 2: Declaration with Definition

C

struct Point {

    int x;

    int y;

} p1, p2;

Accessing Structure Members

Use the dot (.) operator to access the members of a structure.

Example:

C

struct Point {

    int x;

    int y;

};

int main() {

    struct Point p1;

    p1.x = 10;

    p1.y = 20;

    printf("x = %d, y = %d\n", p1.x, p1.y);

    return 0;

}

Nested Structures

Structures can contain other structures as members.

Example:

C

struct Point {

    int x;

    int y;

};

struct Rectangle {

    struct Point topLeft;

    struct Point bottomRight;

};

int main() {

    struct Rectangle rect = {{0, 0}, {10, 10}};

    printf("Top Left: (%d, %d)\n", rect.topLeft.x, rect.topLeft.y);

    printf("Bottom Right: (%d, %d)\n", rect.bottomRight.x, rect.bottomRight.y);

    return 0;

}

Arrays of Structures

You can create an array of structures to store multiple records.

Example:

C

struct Point {

    int x;

    int y;

};

int main() {

    struct Point points[3] = {{0, 0}, {1, 1}, {2, 2}};

    for (int i = 0; i < 3; i++) {

        printf("Point %d: (%d, %d)\n", i + 1, points[i].x, points[i].y);

    }

    return 0;

}

Pointer to Structure

A structure pointer is used to access members using the arrow operator (->).

Example:

C

struct Point {

    int x;

    int y;

};

int main() {

    struct Point p1 = {10, 20};

    struct Point *ptr = &p1;

    printf("x = %d, y = %d\n", ptr->x, ptr->y);

    return 0;

}

Self-Referential Structures

A structure can have a pointer to another structure of the same type.

Example:

C

struct Node {

    int data;

    struct Node *next;

};

This is commonly used in data structures like linked lists.

Passing Structures to Functions

Structures can be passed to functions by value or by reference.

By Value:

C

void display(struct Point p) {

    printf("x = %d, y = %d\n", p.x, p.y);

}

int main() {

    struct Point p1 = {10, 20};

    display(p1);

    return 0;

}

By Reference:

C

void display(struct Point *p) {

    printf("x = %d, y = %d\n", p->x, p->y);

}

int main() {

    struct Point p1 = {10, 20};

    display(&p1);

    return 0;

}

Advantages of Structures

  • Allows grouping of related data.
  • Supports data abstraction and encapsulation.
  • Simplifies program organization.
  • Facilitates the implementation of complex data models.

Limitations of Structures

  • Cannot have member functions like classes in object-oriented programming.
  • No direct support for data hiding.
  • Limited to static memory allocation unless combined with pointers.

Conclusion

Structures are a foundational feature of the C language, enabling efficient and organized handling of complex data. They pave the way for implementing advanced data structures like arrays, linked lists, stacks, and queues.