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[]) {
    char* string_on_data_segment = "ice";
    // String on data segment:
    // - Declared by char* «name» = "…";
    //
    // - Any string literal (e.g., "…") refers to characters on the data segment
    //   (unless it is being used to initialize a string on the stack)
    //
    // - CAN assign a string literal ("…") to a char* because the string literal
    //   really just represents the address of those characters on the data segment.
    //   OK:  string_on_data_segment = "dice";
    //
    // - CanNOT write to individual characters within a string on the data segment.
    //   X    string_on_data_segment[0] = 'a';  ERROR

    char  string_on_stack[] = "salami";
    // Sting on stack...
    // - Declared using [] like this:  char «name»[] = "…"; // or sometimes … = { … };
    //
    // - Type is char[…]
    //
    // - Writable (i.e., can modify characters within the string)
    //   OK: string_on_stack[0] = 'f';
    //
    // - CanNOT assign an entire string to a string on the stack:
    //   X:  string_on_stack = "sailing";  // ERROR
    
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

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