Introduction to C programming

Intro to C

Procedural vs Object-oriented

C is less โ€œsafeโ€

Built-in data types

Intro to Pointers

Pointers in C

p = &i; // p points to i now
\*p = 5; // \*p is another name for i

Pointers as function arguments

Memory Mapping

Twoโ€™s complement

Sign extension

When extending a two's complement value, we don't append zeros to the left, we append the value of the left-most bit.

Memory Manipulation with Pointers

Casting

int i = 4;
short s;
short s = 4;
int i;

Structures in C

Composite data types - struct

struct point {
 int x, y;
 // can include other structs
} p1;
struct point p2;
p1.x = 10;
struct point *p2 = &p1;
p2->x = 20;
(*p2).y = 30; // equal to p2->y = 30

What does p1.y translate to in Assembly?

; r1 points to the starting addr of p1
ldr r0, [r1] ; loads p1.x
ldr r0, [r1, #4] ; loads p1.y

Arrays in C

int m[] = {5, 8, 10}; // size fixed to 3
int n[2][10]; // two-dimensional array
 // with 2 rows and 10 cols
point p[4]; // array of 4 structs

More pointer arithmetic

Memory Management

int *p;
if ((p = malloc(n*sizeof(int))) == NULL)
{
 // Error
}
...
free(p); // release the allocated memory

Memory management is different

Memory regions and management

Memory regions in detail

Categories of variables in C