icp/oer/courses/c-basics/sections/11-strings/01-introduction/program.c

20 lines
675 B
C

#include <stdio.h>
int main(int argc, const char *argv[])
{
// The two initializations below are equivalent
// String literals are converted into 0-terminated char arrays.
char word[] = "Hi";
char second_word[] = {'H', 'i', '\0'};
// Lets see if the last byte is actually a null byte:
printf("%x\n", word[2]);
printf("%x\n", second_word[2]);
// Standard library functions such as printf relay on the null-termination
char faulty[] = {'H', 'e', 'l', 'l', 'o'};
// This might segfault
printf("%s\n", faulty);
// TODO: fix the char array definiton to avoid our programm segfaulting.
// Why do we get a segmentation fault?
}