24-Aug-2021
Hexadecimal
one kind of presentation of values, like binary and decimal
but its presentation really looks like decimal sometimes, so will use 0x as suffix to show that it is hexadecimal
Example: RGB (red green blue)
Hexadecimal is very convenient to convert to binary because 4th unit of binary would be equal to 1 unit of hexadecimal number.
Data structure
typedef struct{int name; string name;} person ← as long as i rmb?
typedef
1. shorthand or rewritten name for data types
2. define a type in the normal way, then alias it to sth else
rewrite name:
typedef <old name> <new name>
typedef unsigned char byte; ← This one I don’t understand
typedef char* string;
photo a: redefine structure name from car to car_t
could finish it in one line instead of adding 2 lines
originally need to def the struct first (if you didn’t rename the structure in photo a)
now only need to tell the name
Is it necessary to rename for the struct first? ←no, you could simply name it like photo a // name it with typedef without naming it after struct
or just struct without the typedef would still work the same? yes
if you use typedef, the declaration could be shorten like the last photo in the above.
- **********************Pointers******************************
pass data between function
- according to CS50- Lesson 2 Tutorial, except array, data were passed by value (copy) instead of data (data stored)
-if pointer was used instead, the VALUE would be passed instead
-the changes did in function would actually impact the other function (the caller)
Memory ←usually talking about RAM
file lives on the disk drive — hard disk drive/solid-state drive ←will still be there rebooting
Disk drive is storage space
manipulation and use of data can only take place in RAM ← move data from disk to ram ← will lose without electricity/ reboot
Usually array of 8-hit wide bytes
different type of data occupy different size in bytes (more details CS50 lesson 1)
Memory = big array of byte-sized cells
array also called by random access — access individual elements of array by indicating which index location we want (like the mailbox number)
Each location would get its own address
Example:
They will line up things starting from multiply of FOUR (for some reasons)
POINTERS ARE ONLY ADDRESS
green box for putting in integers
when you declare : int k;
now we have a green box called k to store integer
then if you say k=5;
now we put 5 into the green k box
now a new box in greenish colour
when you declare: int* pk;
you name the greenish box pk
then if you say pk = &k;
now you put the address of k into the greenish colour box
What is pointer?
Data item
- value is memory address
- “type” describes the data locate at that memory address
(dont quite understand, it also can state the type that the box is storing? so that is why it is in greenish colour and you need to indicate the data type when declaration?)
Help sharing the data structures/variables (instead of copy // not pass by value) among functions
simplest pointer in C
-points to nothing(NULL) ( could be very useful later on)
(NULL =/= NUL where null means nothing, and NUL is one of the code in ASCII (char))
-pointer should ALWAYS be pointing to NULL if the value is not set yet
you can check the pointer is NULL or not using equality(==) ←HOW????
(by using pointer == NULL)
Instead of creating a pointer from nothing, you can extract the address of variable that already exist through “&” (address extraction operator)
The reason why the array is pass by data: is that it is indeed a type of pointers, so the callee could change the value of caller
Dereferencing
modify or inspect the location to which the pointer points
if pointer-to-char is pc, then *pc is the data stored inside memory address pc.
“ * “ ←dereference operator
when put together with pointer, it means go to the reference(pointer) and accesses the data at that specific memory location, allow you to manipulate the data
dereference is like the action of walking towards(getting access to the value) the address pointer points to, but no just knowing the address
what if you dereference the pointer points to NULL
SEGMENTATION FAULT
it pops out error! why it is good for pointer to point at null?
To tell you there is an error. if you didn’t assign value to the variable after declaration, there is a risk that the variable were assigned with unknown number from the garage value or breaking other program/functions
if you don’t assign anything meaningful to the pointer, just point it towards NULL
crash is better than screw up other program / function
How large of the bytes size of string?
String is only the array of char*, would be 4/8 bytes (every address would be 64bits long; if you are using 32bits system, would be 4 bytes long)
the star would be dereferencing operator → dereference the address
Wrong, pk now get the address of m instead of k, so none of the data changes?