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
47
48
49
50
51
52
53
54
#include <stdio.h>
#include <stdlib.h>

// ****************
// **            **
// **   BROKEN   **
// **            **
// ****************
//
// This example is broken.  Contrary to my prior understanding, you apparently
// cannot initialize an array of int using 'int* array = {…};'.  That works for
// strings (array of char) when you initialize with a string literal, but that
// does not work with an array of int.
//

int main(int argc, char* argv[]) {

    ////////////////////////////////////////////////////////////////////////
    //
    // OK
    //
    int a2[] = {7, 8};  // numbers are on the stack

    ////////////////////////////////////////////////////////////////////////
    //
    // BROKEN
    //
    // This results in a compiler error.
    //
    int* a4  = {7, 8};  // numbers are on the data segment
                        // a4 is an address.  That address is on the stack
    
    ////////////////////////////////////////////////////////////////////////
    //
    // DO NOT DO THIS
    //
    // This one compiles, but the data is still stored on the stack.  I don't
    // understand why.  In any case, this is crazy code and should not be used
    // in ECE 264 (or probably ever).
    //
    int* a6  = (int[2]){7, 8};  // numbers are on the stack (for reasons I don't understand)
                                // a6 is an address.  That address is on the stack

    for(int i = 0; i < 2; i++) {
        printf("a2[i] == %d\n", a2[i]);

        printf("a4[i] == %d\n", a4[i]);  // BROKEN. TODO: Come back to this.
        printf("\n");
    }

    
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

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