mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-27 06:44:07 -06:00
vault backup: 2022-09-05 09:34:46
This commit is contained in:
parent
3bba1a1eae
commit
30bda965a8
69
content/notes/11-struct-and-union.md
Normal file
69
content/notes/11-struct-and-union.md
Normal file
@ -0,0 +1,69 @@
|
||||
---
|
||||
title: "11-struct-and-union"
|
||||
aliases:
|
||||
tags:
|
||||
- cosc204
|
||||
---
|
||||
|
||||
# Struct
|
||||
not a calss
|
||||
- class like *composite data type* called struct
|
||||
- POD plain old data
|
||||
|
||||
## declare
|
||||
```
|
||||
struct <name>{
|
||||
<type><name>;
|
||||
<type><name>;
|
||||
} <variable>;
|
||||
|
||||
struct point_2d {
|
||||
double x;
|
||||
double y;
|
||||
} point;
|
||||
```
|
||||
|
||||
typically an anonymous struct is declared and then given a name with typedef
|
||||
|
||||
```
|
||||
typedef struct {
|
||||
double x;
|
||||
double y;
|
||||
} point_2d;
|
||||
```
|
||||
|
||||
This is the common convention now.
|
||||
|
||||
## initalize
|
||||
```
|
||||
point_2d location = {0.0, 1.0}; //initalizers
|
||||
point_2d location = {.y = 1.0, .x = 0.0}; //designated initalizers
|
||||
```
|
||||
|
||||
## accessing
|
||||
use the `.`
|
||||
|
||||
```
|
||||
point_2d location;
|
||||
|
||||
location.x = 0;
|
||||
location.y = 1;
|
||||
```
|
||||
|
||||
## shallow copy
|
||||
copy the members including pointers
|
||||
- but not the things the pointers point to
|
||||
|
||||

|
||||

|
||||
|
||||
both point to the same memory location.
|
||||
when you change one - they both change.
|
||||
|
||||
## passing to routines
|
||||
passed by value
|
||||
|
||||
```
|
||||
void print(thing object)
|
||||
```
|
||||
|
||||
@ -38,7 +38,7 @@ tags:
|
||||
- [06-6809-programming](notes/06-6809-programming.md)
|
||||
- [07-6809-advanced](notes/07-6809-advanced.md)
|
||||
- [08-intro-to-c](notes/08-intro-to-c.md)
|
||||
|
||||
-
|
||||
# Archive
|
||||
|
||||
# Info
|
||||
|
||||
Loading…
Reference in New Issue
Block a user