mirror of
https://github.com/jackyzha0/quartz.git
synced 2025-12-27 14:54:05 -06:00
vault backup: 2022-09-05 09:49:46
This commit is contained in:
parent
30bda965a8
commit
1c4a02c83c
@ -61,9 +61,74 @@ copy the members including pointers
|
|||||||
when you change one - they both change.
|
when you change one - they both change.
|
||||||
|
|
||||||
## passing to routines
|
## passing to routines
|
||||||
passed by value
|
### by value
|
||||||
|
need to copy the whole objcet, usually slower
|
||||||
```
|
```
|
||||||
void print(thing object)
|
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
|
||||||
|
|
||||||
|

|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user