+ 1
If i define an array of charts as... char arr[2][5]={{"dog"}{"cat"}}... What is stored in unused "reserved" memory?
C arrays
5 Réponses
+ 3
char arr[2][5] = {"dog"};
print arr[0][0]; // 'd'
print (int)arr[0][3]; // 0
0 is NULL character
So nothing gets printed and memory for NULL char is reserved in RAM.
+ 3
This means you reserved a space in heap memory!
+ 1
I checked it like this, in Playground there are null past 'g' of "dog", and also null past 't' of "cat". But the result is different when I checked using C4Droid. So I guess there's not much a guarantee what are stored there (I could be wrong).
#include <stdio.h>
int main()
{
char arr[2][5] = {"cat", "dog"};
for(size_t i = 0; i < 2; i++)
{
for(size_t j = 0; j < 5; j++)
{
printf("%c[%u] ", arr[i][j], arr[i][j]);
}
putchar(10);
}
return 0;
}
+ 1
S Adil 🇦🇫 i understand that, i am asking what is stored in unused memory. I said to compiler that strings are gonna be 5 characters long and in mu case they are just 3 char long. So is the 4 element '\0 ?
+ 1
Ipang Yes, as you stated, not always null is stored but sometimes garbage value is stored.