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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>

// DO NOT FILL IN MEMORY FORM YET.

char* copy_string_to_heap(char const* s) {
    return strcpy( malloc(strlen(s) * sizeof *s), s );
}

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

    char* s_arr_stack[] = { copy_string_to_heap("AB"),
                            copy_string_to_heap("CD"),
                            copy_string_to_heap("EF"),
                            copy_string_to_heap("GH"),
                            copy_string_to_heap("IJ") };

    printf("s_arr_stack[0] == \"%s\"\n\n", s_arr_stack[0]);
    printf("sizeof s_arr_stack == %zd\n", sizeof s_arr_stack);
    printf("sizeof s_arr_stack[0] == %zd\n\n\n", sizeof s_arr_stack[0]);

    size_t num_strings = sizeof s_arr_stack / sizeof s_arr_stack[0];  // RIGHT 
                    // =                 40 / 8    // on our platform
                    // =                    5
    
    //size_t num_strings = sizeof s_arr_star_star / sizeof s_arr_star_star[0];  // WRONG
    printf("num_strings == %zd\n", num_strings);

    //char** s_arr_star_star = &s_arr_stack[0];
    //printf("s_arr_star_star[0] == \"%s\"\n\n", s_arr_star_star[0]);
    //printf("sizeof s_arr_star_star == %zd\n", sizeof s_arr_star_star);
    //printf("sizeof s_arr_star_star[0] == %zd\n", sizeof s_arr_star_star[0]);

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

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