What are Data Structures in C and How to use them

When working in C, you have access to various fundamental data structures that facilitate the storage and management of data. These include:

  1. Arrays:

    • A collection of variables of the same type stored in a contiguous block of memory.
    • Enables storing multiple values in a single variable with access via an index.
  2. Structures:

    • Also known as "structs," structures permit the storage of different data types in a single entity.
    • Defined by creating a type definition and then declaring variables of that type.
  3. Linked Lists:

    • A sequence of nodes, each holding a value and a pointer to the next node.
    • Efficient for frequent insertions or deletions of data.
  4. Trees:

    • A hierarchical set of nodes where each node, except the root, has a single parent.
    • Useful for organizing data hierarchically, like a file system's directory structure.
  5. Hash Tables:

    • Uses a hash function to map keys to indices in an array.
    • Facilitates quick and efficient storage and retrieval of data, suitable for implementing dictionaries.

Use of Struct in C

Structs provide a versatile way to organize data with various applications in C.

Example Code

#include <iostream>
using namespace std;

struct Point {
  int x;
  int y;
};

int main() {
  Point p;  // Declare a variable of type "struct Point"
  p.x = 10;  // Assign a value to the "x" field of the struct
  p.y = 20;  // Assign a value to the "y" field of the struct

  cout << "Point p is at (" << p.x << ", " << p.y << ")\n";  // Print the values of the fields

  return 0;
}

In this example, a struct named "Point" is defined with two integer fields, "x" and "y." A variable "p" of type "struct Point" is declared, values are assigned to its fields, and the field values are then printed.

Additional Uses of Structs in C

  • Defining and Using Structs with Different Data Types:

    // Example with typedef
    typedef struct {
      char name[50];
      int age;
      float grade;
    } Student;
    
  • Defining and Using Structs with Pointers:

    typedef struct {
      char name[50];
      int age;
      float grade;
    } Student;
    
    void printStudent(Student* s) {
      // Print the values of the fields using the arrow operator
      cout << "Student " << s->name << " is " << s->age << " years old and has a grade of " << s->grade << endl;
    }
    
  • Comparing Structs:

    Point p1 = {1, 2};
    Point p2 = {3, 4};
    Point p3 = {1, 2};
    
    if (p1 == p2) {
      cout << "p1 and p2 are equal\n";
    } else {
      cout << "p1 and p2 are not equal\n";
    }
    
    if (p1 == p3) {
      cout << "p1 and p3 are equal\n";
    } else {
      cout << "p1 and p3 are not equal\n";
    }
    
  • Sizing Structs:

    cout << "Size of struct Student: " << sizeof(Student) << " bytes\n";
    
  • Using Structs with Functions:

    Student createStudent(char* name, int age, float grade) {
      Student s;
      strcpy(s.name, name);
      s.age = age;
      s.grade = grade;
      return s;
    }
    
  • Using Structs with Arrays:

    Student students[3] = {
      {"Alice", 18, 90.0},
      {"Bob", 20, 85.0},
      {"Charlie", 22, 80.0}
    };
    
    for (int i = 0; i < 3; i++) {
      cout << "Student " << students[i].name << " is " << students[i].age << " years old and has a grade of " << students[i].grade << endl;
    }
    

These examples showcase the versatility of structs in C for handling different data structures and scenarios.

Next Post Previous Post
No Comment
Add Comment
comment url