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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 | #include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
// okay to copy/adapt
// though know this is pretty straight forward stuff (that is, stuff you might find in the documentation)
void _print_string_comparison(char* str1, char* str2) {
if (str1 == str2) {
printf("Strings addresses are equal\n");
} else {
printf("Strings addresses are not equal\n");
}
// strcmp is not just doing string equality, its also for string sorting
// as a result, we have to check == 0 to check string equality
if (strcmp(str1, str2) == 0) {
printf("Strings contents are equal\n");
} else {
printf("Strings contents are not equal\n");
}
}
int main(int argc, char* argv[]) {
char s_on_stack[] = "abc";
s_on_stack[0] = 'd';
printf("s_on_stack = '%s'\n", s_on_stack);
printf("size of s_on_stack = %ld\n", sizeof(s_on_stack));
printf("size of s_on_stack[0] = %ld\n", sizeof(s_on_stack[0]));
// illegal assignment: cannot modify an array variable with a strin constant
// s_on_stack = "def";
/*
strings.c: In function 'main':
strings.c:12:20: error: assignment to expression with array type
12 | s_on_stack = "def";
| ^
*/
printf("s_on_stack = '%s'\n", s_on_stack);
printf("size of s_on_stack = %ld\n", sizeof(s_on_stack));
printf("size of s_on_stack[0] = %ld\n", sizeof(s_on_stack[0]));
char* s_on_data = "abc";
// illegal operation: causes a seg fault at runtime
// no modifying string constants from the data segment
//s_on_data[0] = 'd';
printf("s_on_data = '%s'\n", s_on_data);
printf("size of s_on_data = %ld\n", sizeof(s_on_data));
printf("size of s_on_data[0] = %ld\n", sizeof(s_on_data[0]));
s_on_data = "def";
printf("s_on_data = '%s'\n", s_on_data);
printf("size of s_on_data = %ld\n", sizeof(s_on_data));
printf("size of s_on_data[0] = %ld\n", sizeof(s_on_data[0]));
// compiler error: cannot initialize a string on the stack using string another variable
// char s_on_stack2[] = s_on_data;
/*
strings.c: In function 'main':
strings.c:22:30: error: invalid initializer
22 | char s_on_stack2[] = s_on_data;
| ^~~~~~~~~
*/
char* s_addr = s_on_data;
printf("s_addr = '%s'\n", s_addr);
s_addr = s_on_stack;
s_addr[0] = 'a'; // can modify because it *currently* refers to the address of a stack allocated string
printf("s_addr = '%s'\n", s_addr);
_print_string_comparison(s_on_data, s_addr);
/* cannot == compare to a string constant
strings.c: In function 'main':
strings.c:62:20: error: comparison with string literal results in unspecified behavior [-Werror=address]
62 | if (s_addr == "def") {
| ^~
cc1: all warnings being treated as errors
*/
/*
if (s_addr == "def") {
printf("String is equal to string constant\n");
}
*/
char* str1 = "def";
char* str2 = "def";
_print_string_comparison(str1, str2);
char str_on_stack_def[] = "def";
printf("Two strings with the same data, one on data, one on stack\n");
_print_string_comparison(str1, str_on_stack_def);
return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */
|
© Copyright 2024 Alexander J. Quinn & David Burnett This content is protected and may not be shared, uploaded, or distributed.