If i define an array of charts as... char arr[2][5]={{"dog"}{"cat"}}... What is stored in unused "reserved" memory? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula gr√°tis
+ 1

If i define an array of charts as... char arr[2][5]={{"dog"}{"cat"}}... What is stored in unused "reserved" memory?

C arrays

14th May 2020, 5:37 PM
Bernard Sovdat
5 Respostas
+ 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.
14th May 2020, 5:54 PM
Mustafa K.
Mustafa K. - avatar
+ 3
This means you reserved a space in heap memory!
14th May 2020, 5:51 PM
Shahghasi Adil
Shahghasi Adil - avatar
+ 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; }
14th May 2020, 5:58 PM
Ipang
+ 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 ?
14th May 2020, 5:59 PM
Bernard Sovdat
+ 1
Ipang Yes, as you stated, not always null is stored but sometimes garbage value is stored.
14th May 2020, 6:01 PM
Mustafa K.
Mustafa K. - avatar