pointers

int port = 443;
int *p = &port;
printf("%d\n", *p);

Output:

443

& and *

& takes an address * dereferences a pointer A pointer holds an address, not a magical reference to an object’s lifetime

Pointer arithmetic

p + 1 moves by one element, not one byte That is why the pointer type matters Arithmetic past the end of an array is undefined

NULL

Initialize pointers to NULL Check before dereferencing A null pointer crash is annoying, a stale pointer crash is a security bug

Ownership

C has no syntax for ownership Write it in the function contract Say who allocates and who frees

Next

Structs in 08 structs

Posted