1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>

int main(int argc, char* argv[]) {
    
    int n1 = 5;

    int* a_n1 = &n1;
    // a_n1 is declared as a variable holding the "address of an int" (int*)
    // ... and initialized to the "address of n1".

    printf("   n1 == %d\n", n1);
    printf("*a_n1 == %d\n", *a_n1);  // print the value at address a_n1
    printf("\n");

    printf(" a_n1 == %p\n", (void*)a_n1);   // print a_n1
    printf("  &n1 == %p\n", (void*)(&n1));  // print address of &n1
    printf("\n");

    *a_n1 = 7;   // Write 7 at address a_n1.
    printf("*a_n1 = 7\n");  // print the value at a_n1
    printf("--\n");
    printf("   n1 == %d\n", n1);
    printf(" a_n1 == %p\n", (void*)a_n1);   // print a_n1
    printf("\n");

    int** a_a_n1 = &a_n1;
    // a_a_n1 is declared as a variable holding the address to an address to an int.
    // ... and is initialized to the address of a_n1.
    printf(" &a_n1 == %p\n", (void*)(&a_n1));   // print address of a_n1
    printf("a_a_n1 == %p\n", (void*)(a_a_n1));  // print a_a_n1
    printf("\n");

    int*** a_a_a_n1 = &a_a_n1;
    // a_a_a_n1 is declared as a variable holding the address to an address to
    // and address to an int.
    // ... and is initialized to the address of a_a_n1.
    printf(" &a_a_n1 == %p\n", (void*)(&a_a_n1));   // print address of a_a_n1
    printf("a_a_a_n1 == %p\n", (void*)(a_a_a_n1));  // print a_a_a_n1
    printf("\n");
    
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2022 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.