Submitted by Anonymous (not verified) on Sun, 03/10/2013 - 00:10

Unsized Arrays

As you've seen, the size of a dimension is normally given during the declaration of an array. It means that you have to count each element in an array. It could be tedious to do so, though, especially if there are many elements in an array.

The good news is that the C compiler can actually calculate a dimension size of an array automatically if an array is declared as an unsized array. For example, when the compiler sees the following unsized array:

int  list_int[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90};

it will create an array big enough to store all the elements.

TYPE

Likewise, you can declare a multidimensional unsized array. However, you have to specify all but the leftmost (that is, the first) dimension size. For instance, the compiler can reserve enough memory space to hold all elements in the following two-dimensional unsized array:

char list_ch[][2] = {
         `a', `A',
         `b', `B',
         `c', `C',
         `d', `D',
         `e', `E',
         `f', `F',
         `g', `G'};

The program in Listing 12.7 initializes a one-dimensional character unsized array and a two-dimensional unsized integer array, and then measures the memory spaces taken for storing the two arrays.
Listing 12.7. Initializing unsized arrays.

1:  /* 12L07.c: Initializing unsized arrays */
2:  #include <stdio.h>
3:
4:  main()
5:  {
6:     char array_ch[] = {`C', ` `,
7:                        `i', `s', ` `,
8:                        `p', `o', `w', `e', `r',
9:                        `f', `u', `l', `!', `\0'};
10:    int list_int[][3] = {
11:           1, 1, 1,
12:           2, 2, 8,
13:           3, 9, 27,
14:           4, 16, 64,
15:           5, 25, 125,
16:           6, 36, 216,
17:           7, 49, 343};
18:
19:    printf("The size of array_ch[] is %d bytes.\n", sizeof (array_ch));
20:    printf("The size of list_int[][3] is %d bytes.\n", sizeof (list_int));
21:    return 0;
22: }

 

OUTPUT

The following output is obtained by running the executable 12L07.exe:

C:\app>12L07
The size of array_ch[] is 15 bytes.
The size of list_int[][3] is 42 bytes.
C:\app>

 

ANALYSIS
A character unsized array, array_ch, is declared and initialized in lines 6_9. In
lines 10_17, a two-dimensional unsized integer array, list_int, is declared and initialized too.

The statement in line 19 measures and prints out the total memory space (in bytes) taken by the array array_ch. The result shows that the unsized character array is assigned 15 bytes of memory to hold all its elements after compiling. When you calculate the total number of the elements in the character array manually, you find that there are indeed 15 elements. Because each character takes one byte of memory, the character array array_ch takes a total of 15 bytes accordingly.

Likewise, the statement in line 20 gives the total number of bytes reserved in the memory for the unsized two-dimensional integer array list_int. Because there are a total of 21 integer elements in the array, and an integer takes 2 bytes, the compiler should allocate 42 bytes for the integer array list_int. The result printed out by the printf() function in line 20 proves that there are 42 bytes reserved in the memory for the two-dimensional integer array. (If the size of int or char is different on your machine, you may get different values for the sizes of the arrays in the program of Listing 12.7.)

Related Items

C প্রোগ্রামের Constants ও Variables

Constants and Variables

Constant এর value  কখনোই চেঞ্জ হয় না। অন্যদিকে  variable কে ব্যবহার করা হয় ভিন্ন ভিন্ন ভ্যালু কে দেখানোর জন্য ।

ভালো C প্রোগ্রামিং কিভাবে করবে ?

ক্লাস 24 : তুমি এখন যে গুলি করতে পারো

CLASS 24: What You Can Do Now

You're now in the last chapter of this book. In this lesson you'll learn more about the C language from the following topics:

C Preprocessor এর ব্যবহার ও উপযোগিতা

In Chapter 2, "Writing Your First C Program," you learned how to use the #include preprocessor directive to include C header files. Since then, the #include directive has been used in every program in this book.

C প্রোগ্রামিং ও অ্যাডভান্স File অপারেশন

In last hour's lesson you learned the basics of reading and writing disk data files. In this lesson you'll learn more about communication with disk data files. The main topics discussed in this hour are

    Random access to files
    Reading or writing binary data

C প্রোগ্রামিং ও File অপারেশন

In Chapter 5, "Reading from and Writing to Standard I/O," you learned how to read or write characters through standard input or output. In this lesson you'll learn to read data from or write data to disk files. The following topics are discussed in this lesson: