warning : SILLY QUESTION | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

warning : SILLY QUESTION

suppose we allocate memory equal to unit integer i.e 4 bytes in my system...so i can i now pack 4 charcaters(since 1 char is one byte) in that allocated space on the integer...its like buying a large container which was meant to store water and then storing small containers filled with water inside it..i hope i could make you understand :)

21st May 2022, 10:32 AM
blueshipswims
blueshipswims - avatar
1 Answer
+ 2
hmmm The answer is: yes. C has many ways to accomplish this. Here is one way as proof of concept: int n; char *c = (char*)&n; c[0] = 'H'; c[1] = 'i'; c[2] = '!'; c[3] = '\0'; puts((char*)&n); // using int to print string // output: Hi! ----- Another way: int n = ('H') + ('i'<<8) + ('!'<<16) + ('\0'<<24); puts((char*)&n); //output: Hi! ----- And there is also the aforementioned union data structure (see FF9900's comment), which you can search out next to study.
22nd May 2022, 1:28 AM
Brian
Brian - avatar