-2

I've been trying to use for loops to try to iterate and print out the array. When i try to print out i get these random numbers and i dont know where they are coming from.

 const char segments[10][3][3] =
        {
                {{' ', '_', ' '}, {'|', ' ', '|'}, {'|', "_", '|'}}, // segments for 0
                {{' ', ' ', ' '}, {' ', '|', ' '}, {' ', "|", ' '}}, // segments for 1
                {{' ', '_', ' '}, {' ', '_', '|'}, {'|', "_", ' '}}, // segments for 2
                {{' ', '_', ' '}, {' ', '_', '|'}, {' ', "_", '|'}}, // segments for 3
                {{' ', ' ', ' '}, {'|', '_', '|'}, {' ', " ", '|'}}, // segments for 4
                {{' ', '_', ' '}, {'|', '_', ' '}, {' ', "_", '|'}}, // segments for 5
                {{' ', '_', ' '}, {'|', '_', ' '}, {'|', "_", '|'}}, // segments for 6
                {{' ', '_', ' '}, {' ', ' ', '|'}, {' ', " ", '|'}}, // segments for 7
                {{' ', '_', ' '}, {'|', '_', '|'}, {'|', "_", '|'}}, // segments for 8
                {{' ', '_', ' '}, {'|', '_', '|'}, {' ', "_", '|'}}, // segments for 9
        };


for (int k = 0; k < sizeof(segments); k++){
    for(int j = 0; j < sizeof(segments); j++){
        for (int i = 0; i < sizeof(segments); ++i) {
            printf("%c", segments[k][j][i]);
        }
    }
    printf("\n");
}

When i run the code it gives me an output of something like this:

|_| �| _ |_ �| _ |_ |�| _ | �| _ |_||�| _ || �|�#�z���Z��ث�|| �| _ |_ �| _ |_ |�| _ | �| _ |_||�| _ |_| �|�#�z���Z��ث� �| _ |_ �| _ |_ |�| _ | �| _ |_||�| _ |_| �|�#�z���Z��ث� _ |_ �| _ |_ |�| _ | �| _ |_||�| _ || �|�#�z���Z��ث�| �| _ |_ |�| _ | �| _ |_||�| _ |_| �|�#�z���Z��ث� �| _ |_ |�| _ | �| _ |_||�| _ |_| �|�#�z���Z��ث� _ |_ |�| _ | �| _ |_||�| _ || �|�#�z���Z��ث�| |�| _ | �| _ |_||�| _ |_| �|�#�z���Z��ث�|�| _ | �| _ |_||�| _ |_| �|�#�z���Z��ث� _ | �| _ |_||�| _ |_| �|�#�z���Z��ث� | �| _ |_||�| _ |_| �|�#�z���Z��ث� �| _ |_||�| _ |_| �|�#�z���Z��ث�@ _ |_||�| _ || �|�#�z���Z��ث�@�Z|||�| _ |_| �|�#�z���Z��ث�@�Z|�| _ |_| �|�#�z���Z��ث�@�Z _ || �|�#�z���Z��ث�@�Zұ4|| �|�#�z���Z��ث�@�Zұ4Y �|

nbafanatic
  • 23
  • 5

1 Answers1

2

sizeof(segments) gets the size of the entire array in memory, not the length of any of its axes.

As per the top answer to this question, you can still use sizeof, you just need to use it on different parts of the array: to get the length of each axis, divide its total length by the length of one of its elements.

some_type_t const array[I][J][K] = ARRAY_INITIALIZER;

// array size over size of subarray
size_t const i_max = sizeof(array) / sizeof(array[0]);

// subarray size over size of sub-sub-array
size_t const j_max = sizeof(array[0]) /
                     sizeof(array[0][0]);

// sub-sub-array size over size of one element
size_t const k_max = sizeof(array[0][0]) /
                     sizeof(array[0][0][0]);

for (size_t i = 0; i < i_max; ++i) {
    for (size_t j = 0; j < j_max; ++j) {
        for (size_t k = 0; k < k_max; ++k) {
            // do something with array[i][j][k]
        }
    }
}

From the same answer, you can use the following macro to generate those expressions automatically.

#define NELEMS(x)  (sizeof(x) / sizeof((x)[0]))

int a[17];
size_t n = NELEMS(a);

Why it works

You might expect sizeof(array[0]) to fail on a zero-length array, which might make you hesitate to use it. But don't worry—this should work for any (non-variable-length) array.

Page 80 of this draft of the C standard describes how the sizeof operator works:

If sizeof is given anything besides a variable-length array, it doesn't evaluate it—the compiler can find out how big the operand is and it just uses that value.

So, after I defined array in the example above, the compiler knows that any element of array has type "length-J array of length-K array of some_type_t" (in other words, some_type_t[J][K]). It also knows the values of J and K, because they need to be inline constants, so it can determine how big any element of the array is.

As a result, it doesn't matter what index you use to find the size of the element, as long as it is an integer. sizeof(array[0]) will work just fine with an empty array, as would sizeof(array[-1]) or sizeof(array[124124]) (though you should always use 0 to avoid confusing people).

As a matter of fact, the standard draft even uses it as an example:

  1. EXAMPLE 2 Another use of the sizeof operator is to compute the number of elements in an array:
    sizeof array / sizeof array[0]
    

And if the standard suggests it, it's probably a pretty good way to do it.

jirassimok
  • 3,850
  • 2
  • 14
  • 23