Here you are allocating enough memory for 5 char
s.
char **s = malloc(sizeof(char) * 5);
not however for 5 pointers to char
, which are bigger.
Lets assume pointers are four bytes (could be 8...). Chars are definitly 1 byte.
Here you are writing into that memory, something of 4 bytes (because of the size of a pointer, not because of the four characters).
Not a problem yet.
s[0] = "1101";
Here you are writing into memory which is on a 4 byte higher address.
That adress itself is not yet a problem, it is still within the 5 allocated bytes.
However, writing something larger than one byte there (which you do, size 4) is already incorrect.
s[1] = "1001";
Even worse here, 8 bytes behind the address you got, 3 bytes behind what is alled and a total of 4 bytes. Very incorrect.
s[2] = "0001";
You could already get problems for the second write.
Not encountering problems for second and third access is pure luck. Good or bad luck, depending on your philosophy on when you prefer to find errors.
To solve, allocate memory for the size of what you are writing. Quoting from Barmar comments:
sizeof(char) should be sizeof(char *)
If the argument to sizeof in malloc is a type, it should always have one less * than the type you're assigning to.
From Deduplicator (I actually prefer this, too):
I would much prefer to see char** s = malloc(5 * sizeof *s);. Not having to write the type makes it less error-prone.
The part of not having to type the type includes the potentially wrong part of the number of pointers/asterisks. I.e. this best practice would have prevented your problem.