AnyCompiler

Structs

Ek struct related fields ko ek named type ke neeche group karta hai. Java, C++, ya is site ki har doosri language ki class ke ulat, ek struct mein koi methods, koi constructors, aur koi access control nahi hota — ye purely data ko saath bundle karne ka ek tarika hai, aur har field directly accessible hota hai.

#include <stdio.h>

struct Point {
    int x;
    int y;
};

int main() {
    struct Point p = {3, 4};
    printf("%d %d\n", p.x, p.y);
    return 0;
}
Output
3 4

Aage: dynamic memory — malloc, free, aur heap: wo mechanism jise automate karne ke liye har doosri language ka garbage collector exist karta hai.

Practice exercise

Ek struct Book define karo jiske fields title (size 50 ka char array) aur pages (int) hon. Title "Dune" aur pages 412 wali ek Book banao, phir dono ko printf se print karo.