CSci-E26 Section Notes 2020-10-05

Table of Contents

1 Int pointers

// The assert package will verify that the assertions are true and
// abort the program if not, printing the assertion that failed.
//
#include <assert.h>

int n = 5;
int *x = &n;
int **y = &x;

assert(y == &x);
assert(*y == x);
assert(**y == n);

assert(*&x == x);

Here's a diagram assuming that n is at address 100, x at 104, and y at 112:


        +-------------------+
n: 100  |                  5|
        +-------------------+
                   ^
                   |
                   |
        +-------------------+
x: 104  |                100|<---+
        +-------------------+    |
                                 |
        +-------------------+    |
y: 112  |                104|----+
        +-------------------+

2 Arrays and Sizes

Note that C of course does not allow chained == expressions below.

char s[] = "abc";
char s[] = { 'a', 'b', 'c', '\0' };     // same as above

sizeof(s) == 4 == 4 * sizeof(char);

int  b[] = { 1, 2, 3, 4 };

(unsigned long) b == (unsigned long) &b == (unsigned long) &b[0];

&b[0] == b + 0 == b;

sizeof(b) == 4 * sizeof(int);

sizeof(b)/sizeof(*b) == 4;

sizeof(b) == 16; sizeof(b[0]) == 4;  // asuming sizeof(int) == 4

Author: Alexis Layton

Created: 2020-10-05 Mon 21:16

Validate