C program
Char 1 byte
Int 4
Float 4
Double 8
Pointer 8 bytes
1. Consider this
char words[] [20] ={“this_is_a_long_line”, “gr”, “ye”, “red”, “blue”} ;
What is the amount of memory allocated for words? (ie sizeof(words)
2. Consider
char * wordsP[] ={“this_is_a_long_line”, “gr”, “ye”, “red”, “blue”} ;
What is the amount of memory allocated to wordsP?
3. What is the total amount of memory allocated for the declaration of wordsP? (ie memory + element strings)
Expert Answer
#include<stdio.h>
int main() {
char words[] [20] ={“this_is_a_long_line”, “gr”, “ye”, “red”, “blue”} ;
printf(“%lu”,sizeof(words));
}
Result:
100
The reason being is that there are 5 strings in the character array each with size “20” specified in array.
So 5*20 i.e 100.
#include<stdio.h>
int main() {
char * wordsP[] ={“this_is_a_long_line”, “gr”, “ye”, “red”, “blue”} ;
printf(“%lu”,sizeof(wordsP));
}
Result:40
In the question pointer size is 8. We have 5 strings. So 5*8 is 40.
Total amount memory allocated element strings is this_is_a_long_linegryeredblue is 31. ‘