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 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
void print_string(char* s) {
printf("\"%s\" # inside print_string(…)\n", s);
}
int main(int argc, char* argv[]) {
char s_stack[] = "ABC";
printf("s_stack == %s\n", s_stack);
s_stack[2] = 'A';
printf("s_stack == %s # after modification\n", s_stack);
// s_stack = "XYZ"; // GCC ERROR: assignment to expression with array type
// // .. because you can only assign to a specific element of an array
char* s_data_segment = "ABC";
printf("s_data_segment == %s\n", s_data_segment);
//s_data_segment[2] = 'A'; // RUNTIME ERROR: Segmentation fault
// // .. because we tried to write to a read-only portion of the data segment
s_data_segment = "XYZ";
printf("s_data_segment == %s # after setting to \"XYZ\"\n", s_data_segment);
print_string(s_data_segment);
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.