mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-28 07:14:05 -06:00
171 lines
3.1 KiB
Markdown
171 lines
3.1 KiB
Markdown
---
|
|
title: "11-struct-and-union"
|
|
aliases:
|
|
tags:
|
|
- cosc204
|
|
- lecture
|
|
sr-due: 2022-9-20
|
|
sr-interval: 16
|
|
sr-ease: 250
|
|
---
|
|
|
|
# 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
|
|
### by value
|
|
need to copy the whole objcet, usually slower
|
|
```
|
|
void thing_print(thing object) {
|
|
print("%f ",object.value)
|
|
print("%f\n", object.string)
|
|
object.value = object.value -1; //does not affect first because it is a copy of object
|
|
}
|
|
|
|
thing_print(first)
|
|
```
|
|
|
|
### by address
|
|
usually faster. less data coming off and onto the stack
|
|
```
|
|
void thing_print(thing *object) {
|
|
print("%f ",object->value) //struct->field === (*struct).field
|
|
print("%f\n", object->string)
|
|
object->value = object->value -1; //does not affect first because object is a pointer to first
|
|
}
|
|
|
|
thing_print(&first)
|
|
```
|
|
|
|
## returning
|
|
- return by value
|
|
- never return a pointer to somthing on the stack
|
|
|
|
# Queue
|
|
- enqeue to tail
|
|
- dequeue from head
|
|
- FIFO
|
|
|
|

|
|
|
|
## enqueue with pointers
|
|
queue is a linked list
|
|
`NULL` as sentinel value at end
|
|
|
|

|
|
|
|
- go down list till we find null
|
|
- create new object (`malloc`), value = 123, pointer = NULL
|
|
- change null in final object to the new object
|
|
|
|
```
|
|
typedef struct q_item {
|
|
int value;
|
|
stuct q_item *next;
|
|
} queue_item;
|
|
|
|
//for convenience we have a struct for the queue
|
|
typedef struct {
|
|
queue_item *head;
|
|
} queue;
|
|
```
|
|
|
|
constructor
|
|
```
|
|
queue *queue_new(void){
|
|
queue *this = malloc(sizeof(queue));
|
|
this->head = NULL;
|
|
return this;
|
|
}
|
|
```
|
|
|
|
enqueue
|
|

|
|
|
|
dequeue
|
|

|
|
|
|
# Structs as view
|
|
```
|
|
typedef struct q_item {
|
|
int value; //4-byte integer
|
|
struct q_item *next; //8-byte pointer
|
|
} queue_item;
|
|
```
|
|
|
|

|
|
|
|
struct is a view where the members are laid out consecutively
|
|
|
|
# Union
|
|
multiple views of the same memory
|
|
- each line in the declaration is a different view
|
|
|
|
```
|
|
typedef union {
|
|
struct {
|
|
uint8_t a, b, c, d;
|
|
};
|
|
uint32_t integer;
|
|
} two_views;
|
|
```
|
|
|
|

|
|
|
|
## endianess
|
|
big-endian vs little-endian
|
|
some computer store the most significatn byte first, other store the least significant byte first
|
|
|