Home > front end >  Do arrays end with NULL in C programming?
Do arrays end with NULL in C programming?

Time:12-26

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);
}

CodePudding user response:

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.

CodePudding user response:

In C programming, arrays do not typically end with a null value. Arrays are a contiguous block of memory that holds a fixed number of elements of the same type. The size of the array is determined when it is created and cannot be changed.

To access the elements of an array, you can use indexing. The first element of the array is at index 0, the second element is at index 1, and so on. The size of the array is not stored with the array, so you have to keep track of it yourself or use a separate variable to store it.

If you want to represent a list of elements that can vary in size and that is terminated by a special value, you can use a linked list or a null-terminated string instead. A null-terminated string is a sequence of characters that is terminated by a null character ('\0')

CodePudding user response:

> 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.

  • Related