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 s_on_stack[] = "abc";
s_on_stack[2] = 'd';
printf("%s\n", s_on_stack);
// invalid initializer
// char s_on_stack2[] = s_on_stack; <-- compile error
char* s_on_data = "abc";
// cannot modify strings stored in data segment
//s_on_data[2] = 'd'; <-- seg fault at runtime
printf("%s\n", s_on_data);
char* s_on_data2 = s_on_data;
printf("%s\n", s_on_data2);
printf("s_on_data = %p\ns_on_data_2 = %p\n", s_on_data, s_on_data2);
// can store address of an array
char* s_on_stack_addr = s_on_stack;
printf("%s\n", s_on_stack_addr);
printf("s_on_stack = %p\ns_on_stack_addr = %p\n", s_on_stack, s_on_stack_addr);
// does not work to use array initializer for char*
//char* s = { 'A', 'B', 'C', '\0'};
// can use array initializer for array of char*
char* s[] = {"hello", "world"};
return EXIT_SUCCESS;
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.