Struct
- C
- C++
- C#
- Java
- Rust
- Go
- Javascript
- Typescript
- Python
// ------------------------------------
// Definition
// ------------------------------------
struct Struct_name {
some_type variable_name;
some_type variable_name;
...
some_type variable_name;
} struct_alias;
// ------------------------------------
// Declaration Example
// ------------------------------------
struct Person
{
char name[50];
int age;
};
// ------------------------------------
// Assignment
// ------------------------------------
struct Person some_person; // Either use "struct myStruct" or a type alias
strcpy(some_person.name, "my name"); // strcpy() from <string.h>
some_person.age = 30;
// ------------------------------------
// Others
// ------------------------------------
// You can also return a struct from a function
struct Person getSomeone(void)
{
// ...
}
More Info:
// ------------------------------------
// Definition
// ------------------------------------
struct Struct_name
{
some_type variable_name;
some_type variable_name;
...
some_type variable_name;
} struct_alias;
// ------------------------------------
// Declaration Example
// ------------------------------------
struct Person
{
std::string name;
std::uint8_t age;
};
// ------------------------------------
// Assignment
// ------------------------------------
Person some_person; // Just like a Class
some_person.name = "my name";
some_person.age = 30;
// Since C++11 you can also do this way
Person some_person{"my name", 30};
// ------------------------------------
// Others
// ------------------------------------
// You can also return a struct from a function
Person getSomeone()
{
return {"Someone", 35}
}
More Info:
struct Point
{
public int X { get; set; }
public int Y { get; set; }
public Point(int x, int y)
{
X = x;
Y = y;
}
}
Point point = new Point(10, 20);
// Since C# 10
readonly record struct Size(int Width, int Height);
// No Native Struct Support.
// Use a class or record. Records are available since Java 16.
record Point(int x, int y) {}
Point point = new Point(10, 20);
int x = point.x();
struct Point {
x: i32,
y: i32,
}
let point = Point { x: 10, y: 20 };
// Tuple struct
struct Color(u8, u8, u8);
let color = Color(255, 0, 0);
type Point struct {
X int
Y int
}
point := Point{X: 10, Y: 20}
point.X = 30
// Anonymous struct
user := struct {
Name string
Age int
}{
Name: "Ada",
Age: 36,
}
// ------------------------------------
// Definition
// ------------------------------------
// Javascript doesnt have Structs, but you can use "Object Literal" notation
// Remember that in Javascript, objets are reference values
const object_name = {
key_name: value_name,
key_name: value_name,
...
key_name: value_name,
};
// ------------------------------------
// Declaration Example
// ------------------------------------
// You can start without any value
const some_person = {
name,
age,
};
// ------------------------------------
// Assignment
// ------------------------------------
some_person.name = "my name";
some_person.age = 30;
// you can also use a bracket notation
some_person["name"] = "my name";
some_person["age"] = 30;
// ------------------------------------
// Others
// ------------------------------------
// You can also return a object from a function
// Arrow Function Style
const getSomeone = () => {
return {
name: "Someone",
age: 35,
}
}
// Old function style
function getSomeone() {
return {
name: "Someone",
age: 35,
}
}
// No Native Struct Support.
// Use type aliases or interfaces for object shapes.
type Point = {
x: number;
y: number;
};
const point: Point = { x: 10, y: 20 };
interface Size {
width: number;
height: number;
}
from dataclasses import dataclass
@dataclass
class Point:
x: int
y: int
point = Point(10, 20)
# Alternative lightweight immutable tuple-like type
from typing import NamedTuple
class Size(NamedTuple):
width: int
height: int