I don't know the inner workings of c so this is quite confusing. I've been playing with arrays and pointers and a lot of weird stuff is going on, I hope someone can point me to a resource that explains how this stuff works under the hood. I was looking at the c programming reference but could not find answers.
So here is a piece of code:
#define PATH "/home/jack/Desktop/Cpractice/hangman.txt"
file = fopen(PATH, "r");
//char *word = (char *)malloc(0);
int i =0;
c = getc(file);
if(c == EOF)
word[i] = c;
printf("I: %i\n", i);
It's my first comment so i'll try to be as clear as possible.
To be quite short, the memory in C language is a bit special, but not complicated until you dig a bit the subject.
The malloc function (dynamic allocation) and static allocation uses a system call : sbrk (I advise you to read the man to understand what exactly the function does) https://linux.die.net/man/2/sbrk
Your question is "why can I read tab[size + 1]", it's juste because the memory given to your array isn't only size, in fact, if there is free space after, you'll be able to access it, but BE CAREFUL because this will probably lead to some errors in your program after some time...
Errors are possible because if you declare another array after this one, let's say an array of 6 chars named tab2, and in memory this array starts right after the other one, when you'll try to modify tab1[size + 1] you'll got a segmentation fault because the space is used by tab2 (even reading tab1[size + 1] could lead to a segmentation fault, but sometimes, computer are too kind and let you do that)
Wathever, I hope it's quite clear, if it isn't, don't hesitate to ask me questions !