1

I am a beginner to C and I was asked to calculate size of an array without using sizeof operator. So I tried out this code, but it only works for odd number of elements. Do all arrays end with NULL just like string.

#include <stdio.h>
void main()
{
    int a[] = {1,2,3,4,5,6,7,8,9};
    int size = 0;
    for (int i = 0; a[i] != '\0'; i++)
    {
        size++;
    }
    printf("size=%d\n", size);
}
user3840170
  • 26,597
  • 4
  • 30
  • 62
  • Does this answer your question? [How does this piece of code determine array size without using sizeof( )?](https://stackoverflow.com/questions/56154380/how-does-this-piece-of-code-determine-array-size-without-using-sizeof) – Harith Dec 26 '22 at 09:20
  • 2
    Note that `'\0'` and `NULL` are not same. – H.S. Dec 26 '22 at 10:13
  • 1
    Not like strings. A string may be contained in a char array, but the nul does not indicate the end of the array, it indicates the end of the string, which may be shorter. It would be a misunderstanding to think string arrays end in nul, the string ends in nul, not necessarily the _containing_ array, which is just an array. Strings are not a data type in C, they are a _convention_. – Clifford Dec 26 '22 at 10:18
  • 1
    The ASCII mnemonic for the null character is `NUL`. The C macro `NULL` refers to a null pointer, and it is well not to confuse the two. – Clifford Dec 26 '22 at 10:21

2 Answers2

5

No, in general, there is no default sentinel character for arrays.

As a special case, the arrays which ends with a null terminator (ASCII value 0), is called a string. However, that's a special case, and not the standard.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • What could be the reason that this code is working for odd numbers and not even number? – Pranav Vasishta Dec 26 '22 at 10:17
  • Undefined behavior. You're reading past the valid memory in your code. – Sourav Ghosh Dec 26 '22 at 10:18
  • A string is terminated by nul, not the containing array. There is no "special case" in that sense. The array may be longer than the string, and the end of the array need not contain a nul. – Clifford Dec 26 '22 at 10:26
  • Unless, it's initialised upon declaration and definition. Then, we can be certain that the array end contains null. Right? – Sourav Ghosh Dec 26 '22 at 10:30
0

> So I tried out this code, but it only works for odd number of elements.

Try your code with this array -

int a[] = {1,2,0,4,5,6,7,8,9}; 
               ^
               |
          3 replaced with 0

and you will find the output will be size=2, why?
Because of the for loop condition - a[i] != '\0'.

So, what's happening when for loop condition hit - a[i] != '\0'?
This '\0' is integer character constant and its type is int. It is same as 0. When a[i] is 0, the condition becomes false and loop exits.

In your program, none of the element of array a has value 0 and for loop keep on iterating as the condition results in true for every element of array and your program end up accessing array beyond its size and this lead to undefined behaviour.

> Do all arrays end with NULL just like string.

The answer is NO. In C language, neither array nor string end with NULL, rather, strings are actually one-dimensional array of characters terminated by and including the first null character '\0'.

To calculate size of array without using sizeof, what you need is total number of bytes consumed by array and size (in bytes) of type of elements of array. Once you have this information, you can simply divide the total number of bytes by size of an element of array.

#include <stdio.h>
#include <stddef.h>

int main (void) {
    int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    ptrdiff_t size = ((char *)(&a + 1) - (char *)&a) / ((char *)(a + 1) - (char *)a);

    printf("size = %td\n", size);
    return 0;
}

Output:

# ./a.out
size = 9

Additional:

'\0' and NULL are not same.

H.S.
  • 11,654
  • 2
  • 15
  • 32