Recent Changes - Search:

Oktatás

* Programozás 2
  + feladatsor
  + C feladatsor
  + Python feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

* Adator. prog.
  + feladatsor
  + quick link

Teaching

* Prog. for Data Sci.
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/1
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Structs

Structs are value types!

(1) structs
#include <stdio.h>

typedef struct
{
    int x;
    int y;
} Point;

int main()
{
    Point a;
    a.x = 1;
    a.y = 2;

    Point c = {6, 5};

    Point d = {x : 6, y : 5}; // designated initializer
    printf("%d\n", d.x);      // 6

    Point e = {y : 5, x : 6}; // notice the shuffled order
    printf("%d\n", e.x);      // 6

    printf("A(%d, %d)\n", a.x, a.y); // A(1, 2)

    printf("C(%d, %d)\n", c.x, c.y); // C(6, 5)

    printf("%d\n", sizeof(Point)); // 8 (i.e., 8 bytes)

    return 0;
}
import std.stdio;

struct Point
{
    int x;
    int y;
}

void main()
{
    Point a;
    a.x = 1;
    a.y = 2;

    Point b = Point(3, 4);

    Point c = {6, 5}; // C-style

    Point d = {x: 6, y: 5}; // designated initializer
    writeln(d); // Point(6, 5)

    Point e = {y: 5, x: 6}; // notice the shuffled order
    writeln(e); // Point(6, 5)

    writefln("A(%s, %s)", a.x, a.y); // A(1, 2)

    writefln("B(%s, %s)", b.x, b.y); // B(3, 4)

    writefln("C(%s, %s)", c.x, c.y); // C(6, 5)

    writeln(a); // Point(1, 2)

    writeln(Point.sizeof); // 8 (i.e., 8 bytes)
}

Notes:

  • Variables of struct and class types are called objects.

(2) const and immutable
void main()
{
    const Point a = Point(1, 2);

    immutable Point b = Point(3, 4);

    // a.x = 10; // error
    // b.x = 30; // error
}

It works as expected. The objects cannot be changed.

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 September 22, 13:51