-1

I have seen some C programs use the sizeof operator to bound loops while iterating over an array. What is the correct method for doing this? I have the example as follows:

#include <stdio.h>

int main(void)
{
    size_t i;
    char a[100];
    /* Edited '<=' to '<' as suggested */
    for ( i = 0;i < sizeof a / sizeof *a; ++i)
        a[i] = i;

    for ( i = 0;i < sizeof a / sizeof *a; ++i)
        printf("%d\n", a[i]);
    return 0;
}
grafos
  • 7
  • 4
  • sizeof() gives you the (byte) size of a single object of given type, ex: int, bool, etc... this might help: http://en.cppreference.com/w/cpp/language/sizeof – Evan Carslake Feb 13 '16 at 23:35
  • 5
    it needs `<` instead of `<=`. Other than that, your version looks good. Using parentheses after `sizeof` is also advisable. – Petr Skocik Feb 13 '16 at 23:39
  • 4
    There's nothing wrong with this use of `sizeof` (except that `sizeof char` is by definition 1 anyway), but your `<=` should be `<`. –  Feb 13 '16 at 23:39
  • http://stackoverflow.com/a/18078435/887584 This has been answered before, and it mentions that it will not work for pointer types that aren't arrays. – Emil Feb 13 '16 at 23:43
  • Do you have some reason for doubting the correctness of the example? Do you have some uncertainty about the effect of the `sizeof` operator? Do you think there can be only one correct way of doing what you ask? I can't help getting the impression that the question you have asked does not express well what you really want to know. – John Bollinger Feb 13 '16 at 23:49

2 Answers2

1

A safer method would be to calculate the size of the array once. Repeating the calculation has the possibility of injecting more defects (such as typos).

#include <stdio.h>

int main(void)
{
    size_t i;
    char a[100];
    const unsigned int array_size = sizeof(a) / sizeof(a[0]);
    for ( i = 0;i < array_size; ++i)
    {
        a[i] = i;
    }
    for ( i = 0;i < array_size; ++i)
    {
        printf("%d\n", a[i]);
    }
    return 0;
}

Some notes:

  1. In for loops, the termination is usually "<" (and not "<=") the size because indices start at zero.
  2. The compiler may already calculate sizeof(a) / sizeof(*a) as an explicit constant once and repeat its value, but I created a temporary variable to be explicit.
  3. The C and C++ languages are different languages, use your language tags appropriately (since you talk about C not C++).
  4. I suggest using the term "capacity" for the total number of elements, because size can mean the number of occupied slots in an array (a capacity 10 array may have 4 slots filled, size == 4).
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
-1
// Your way works but I'd change it a little for clarity. (I also fixed the <= issue)

#include <stdio.h>
#define NUM_ELEMENTS 100
int main(void)
{
    size_t i;
    char a[NUM_ELEMENTS];
    for ( i = 0;i < NUM_ELEMENTS; ++i)
        a[i] = i;

    for ( i = 0;i < NUM_ELEMENTS; ++i)
        printf("%d\n", a[i]);
    return 0;
}
nicomp
  • 4,344
  • 4
  • 27
  • 60