[prev] [index] [next]

Iteration over Arrays

Two approaches to array iteration:
  • count-controlled, using size of array, e.g.

    int i;
    int v[10];
    for (i = 0; i < 10; i++) {
    	printf("%d\n", v[i]);
    }
    

  • using a terminating value, e.g.

    int i;
    char s[10];
    // somehow s[] is set to hold a C-string
    for (i = 0; s[i] != '\0'; i++) {
    	printf("%c", s[i]);
    }