I am trying to learn C, as a hobby. Therefore I am creating a long .c file with lots and lots of declaration, etc to see and learn the programming language. My issue is that my program crashes and I am unable to understand why! Here is the code
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
printf("Hello, World!\n");
double Double_Array[5] = {[0] = 9.0, [2] = 5.0, [1] = 7.12, [4] = 3.E+25};
double DoubleArray[] = {0.0007, 0.1, 6};
for(size_t i = 0; i < 5; i++) {
// %zu and %g are what's called "format specifiers"
printf("element␣%zu␣is␣%g,␣\tits␣square␣is␣%g\n", i, Double_Array[i], Double_Array[i]*Double_Array[i]);
}
int int_x[] = {1,2,3}; // x has type int[3] and holds 1,2,3
int int_y[5] = {1,2,3}; // y has type int[5] and holds 1,2,3,0,0
int int_z[3] = {0}; // z has type int[3] and holds all zeroes
char str_array[] = "abc"; // str has type char[4] and holds 'a', 'b'. 'c', '\0'
char str_array3[3] = "abc"; // str has type char[3] and holds 'a', 'b', 'c'
wchar_t wstr[4] = L"猫"; // str has type wchar_t[4] and holds L'猫', '\0', '\0', '\0'
// Ternary (condition) operation
int aaa = 10, bbb = 11;
(aaa > bbb) ? (printf("A is the biggest!\n")) : (printf("B is the biggest!\n"));
int my_single_array[5] = {1, 5, 2, 4, 7};
int my_multidimensional_array[2][3][4] = {
{{9, 1, 8, 3}, {1, 8, 3, 4}, {8, 3, 4, 5}}, // 0
{{8, 4, 8, 3}, {8, 5, 5, 1}, {9, 6, 8, 3}} // 1
}; // 3D
/*
for(int i = 0; i < 2; i++) {
printf("%i\n", my_multidimensional_array[i][i][i]);
}
printf("\nsingle array: %i", my_single_array[1239]);
*/
char* char_A = "A";
const char* char_AA[2] = {"AA"};
char* const char_AAA[50] = {"AAA"};
const char* const char_AAAA[50] = {"AAAA"};
printf("\n\n"
"char_A: %s\n"
"char_AA: %s\n"
"char_AAA: %s\n"
"char_AAAA: %s\n"
" \n\n"
, char_A,char_AA[0], char_AAA[0], char_AAAA[0]);
printf("got here"); // never prints this CRASHES BEFORE THIS
char_A[0] = 'B';
char_AA[0] = "CD";
// char_AAA = 'Changed char_AAA'; // ILLEGAL, reason: constant content, movable pointer
// char_AAAA = 'Changed char_AAAA'; // ILLEGAL, reason: constant content and pointer!
printf("HERE");
}
Why do I get a segmentation fault error? Am I accessing parts of memory I am not suppost to? I have tried not printing out char_A, char_AA, char_AAA and char_AAAA but the program still crashes after the print despite not accessing the variables.