Why the NULL character is not right after the last index of this array?

Walkbitterwyrm Lightwarg 20 Reputation points
2023-10-31T14:11:57.9366667+00:00

I am using the following program to check the position of the NULL character in a character array. Usually it should be at the 'n; index of an array with size 'n'. But this program shows the NULL character at n+1 index.

#include <stdio.h>

int main()
{
    char a[5] = {'a', 'b', 'c', 'd', 'e'};

    printf("Character at index 5: %c\n", a[5]);

    printf("Character at index 6: %d", a[6]);

    return 0;
}

Output:

Character at index 5: Ç

Character at index 6: 0

What is giving such an output and why the 'Ç' character is at index 5?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,650 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,637 questions
{count} votes

6 answers

Sort by: Most helpful
  1. WayneAKing 4,921 Reputation points
    2023-11-09T10:52:36.1966667+00:00

    >That's not the reality because the null character should stay after the last index element, even if the array is full or empty.

    Who says it should? Where in the ANSI/ISO Standard for the C language do you read that?

    I believe you have been told before that the only valid index values for accessing array elements are 0 to n - 1 where n is the number of elements in the array. So for an array of size 5 the only valid indexes are 0, 1, 2, 3, 4.

    Using any other value for the index is a programming error, and will result in compile errors or run time errors. If the compiler and build type used is lenient it may not give any error or warning. If it has robust checking then it will usually notify you of index values out of range.

    As not all compilers or build options give notice when an invalid index is used, it is often a good practice to use a static code analyzer to check the program for errors not caught by the compiler. Visual Studio includes the "Analyze" feature to do this type of analysis. Analyzing you program example using VS gives the following:

    Warning C6201 Index '5' is out of valid index range '0' to '4' for possibly stack allocated buffer 'a'.

    Warning C6385 Reading invalid data from 'a': the readable size is '5' bytes, but '6' bytes may be read.

    Warning C6201 Index '6' is out of valid index range '0' to '4' for possibly stack allocated buffer 'a'.

    Similar results will be produced by other static analyzers.

    When you use an invalid index and the compiler does not abort the build, then at run time the program will be accessing a memory location that is not associated with the array. The contents of that memory location may be any possible value, depending on what code or data last used or is using that memory location.

    Note that in this line of your code:

    char a[5] = { 'a', 'b', 'c', 'd' };

    since you have specified an array size of 5 but have only provided 4 characters in the initializer list, the fifth element of the array (at index 4) will be zero-initialized so the last location in the array will contain a nul character.

    Recommended reading:

    Initialization of arrays

    https://www.ibm.com/docs/en/zos/3.1.0?topic=initializers-initialization-arrays

    • Wayne
    0 comments No comments