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

// RULE:  a[i] ↔ *(a + i)

// Same as previous example, except string 's' is on stack segment.

// RULE:  If a is an array of int, then you can use a anywhere that you can use a char*.

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

    char s1[] = "ABCDE"; // s1 is an array of char on stack segment
    printf("s1[0] == '%c'   *(s1 + 0) == '%c'\n", s1[0], *(s1 + 0));
    printf("s1[1] == '%c'   *(s1 + 1) == '%c'\n", s1[1], *(s1 + 1));
    printf("s1[2] == '%c'   *(s1 + 2) == '%c'\n", s1[2], *(s1 + 2));
    
    char* s2 = "ABCDE"; // s2 is the address of a string (array of char) on data segment
    printf("s2[0] == '%c'   *(s2 + 0) == '%c'\n", s2[0], *(s2 + 0));
    printf("s2[1] == '%c'   *(s2 + 1) == '%c'\n", s2[1], *(s2 + 1));
    printf("s2[2] == '%c'   *(s2 + 2) == '%c'\n", s2[2], *(s2 + 2));

    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.