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

int main(int argc, char* argv[]) {
    int numbers[] = { 10, 11, 12 };  // on the stack

    int* a_numbers_0 = &numbers[0];   // &numbers[0] ↔ &(numbers[0])
    // a_numbers_0 is the address of the first element.

    int* a_numbers   = numbers;
    // a_numbers is also the address of the first element.

    printf("numbers[0]     == %d\n", numbers[0]);
    printf("a_numbers_0[0] == %d\n", a_numbers_0[0]);
    printf("a_numbers[0]   == %d\n", a_numbers[0]);

    // What's the difference then????
    char  s1[] = "abc"; // on stack segment because type is char[…].
    char* s2   = "xyz"; // on data segment because type is char* and initialized with "…"

    // Array (char[…]) - you can write to the elements.
    s1[0] = 'b';   // Remember: single quotes for character constants
    //s2[0] = 'z';  // NOT ALLOWED because you cannot write to read-only portion of data
    //              // the data segment.

    // Variable containing address of a char (char* variable)
    s2 = s1;
    // s1 = s2;   // NOT ALLOWED because you cannot assign to an array (all at once)

    
    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.