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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
int main(int argc, char* argv[]) {
char* s1 = "ABC"; // string on the DATA SEGMENT
// s1 is the address of string on the data segment.
// The characters ('A', 'B', 'C') are on the data segment.
// The address where the characters can be found is on the stack segment.
char s2[] = "ABC"; // string on the STACK SEGMENT
// s2 is an array of char's on the stack segment.
printf("s1 == %s\n", s1);
s2[2] = 'E';
printf("s2 == %s\n", s2);
char* s3 = s1;
printf("s3 == %s\n", s3);
char s4[] = s2; // GCC ERROR: "error: invalid initializer"
// DATA SEGMENT
// - Cannot write to a string on the data segment. ⇒ Segmentation fault
// - Can assign an address of a string (char*) to another address of string (char*)
// directly.
//
// STACK
// - CAN write to a string on the data segment.
// - Cannot initialize a char[] with another char[].
//
// BOTH
// - Cannot increase size of a string on the data segment or on the stack.
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.