bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

luc072.c (2157B)


      1 /*
      2  * Author  : Amit Dutta <amitdutta4255@gmail.com>
      3  * Date    : 08 Feb 2026
      4  * Repo    : https://github.com/notamitgamer/bsc
      5  * License : MIT License (See the LICENSE file for details)
      6  */
      7 
      8 /* How many bytes in memory would be occupied by the following array of pointers to strings? How many bytes would be required to store the same strings in a two-dimensional character array?
      9 */
     10 
     11 /* Let Us C, Chap- 16 (Handling Multiple Strings), Qn No.: A(a) */
     12 
     13 /* This file is auto-generated by a bot. */
     14 /* This code is not compiled; it is for reference only. */
     15 
     16 
     17 #include <stdio.h>
     18 #include <string.h>
     19 #include <stdlib.h>
     20 #include <ctype.h>
     21 
     22 int main()
     23 {
     24     /* Question Analysis:
     25        char *mess[] = {"Hammer and tongs", "Tooth and nail", "Spit and polish", "You and C"};
     26        
     27        1. Array of Pointers (*mess[]):
     28           - It stores 4 pointers. 
     29           - Size of a pointer is typically 4 bytes (32-bit) or 8 bytes (64-bit).
     30           - Total = 4 * sizeof(char*)
     31           - Plus the strings themselves are stored elsewhere in memory.
     32        
     33        2. Two-Dimensional Array (mess[][]):
     34           - Must accommodate the longest string ("Hammer and tongs" = 16 chars + null = 17).
     35           - Width would be 17 (or more).
     36           - Size = 4 rows * 17 cols * 1 byte.
     37     */
     38 
     39     char *mess_ptr[] = {
     40         "Hammer and tongs", 
     41         "Tooth and nail", 
     42         "Spit and polish", 
     43         "You and C"
     44     };
     45 
     46     // Longest string length + 1 for null terminator
     47     // "Hammer and tongs" is 16 chars long.
     48     char mess_2d[4][17] = {
     49         "Hammer and tongs", 
     50         "Tooth and nail", 
     51         "Spit and polish", 
     52         "You and C"
     53     };
     54 
     55     printf("--- Memory Occupation Analysis ---\n\n");
     56 
     57     printf("1. Array of Pointers (char *mess[]):\n");
     58     printf("   Size of array object itself (4 pointers): %zu bytes\n", sizeof(mess_ptr));
     59     printf("   (Note: The string literals are stored in read-only memory separately)\n\n");
     60 
     61     printf("2. Two-Dimensional Array (char mess[4][17]):\n");
     62     printf("   Size of 2D array: %zu bytes\n", sizeof(mess_2d));
     63     printf("   (Calculation: 4 rows * 17 columns * 1 byte)\n");
     64 
     65     return 0;
     66 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror