Allen
Allen

Reputation: 182

Maximum number of elements that can be stored in an array in c

Please pardon me if it is a copy question. I will be happy to delete it if pointed out.

The question is that, if I declare a character array in c, say

char character_array[4];

Does that mean I can only store 3 characters and one '/0' is added as the fourth character? But I have tried it and successfully added four characters into the character array. But when I do that where is the '/0' added since I have already used up the four positions?

Upvotes: 0

Views: 3228

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 310930

You mix two notions: the notion of arrays and the notion of strings.

In this declaration

char character_array[4];

there is declared an array that can store 4 objects of type char. It is not important what values the objects will have.

On the other hand the array can contain a string: a sequence of characters limited with a terminating zero.

For example you can initialize the array above in C the following way

char character_array[4] = { 'A', 'B', 'C', 'D' };

or

char character_array[4] = "ABCD";

or

char character_array[4] = { '\0' };

or

char character_array[4] = "";

and so on.

In all these cases the array has 4 objects of type char. In the last two cases you may suppose that the array contains strings (empty strings) because the array has an element with zero character ( '\0' ). That is in the last two cases you may apply to the array functions that deal with strings.

Or another example

char character_array[4] = { 'A', 'B', '\0', 'C' };

You can deal with the array as if it had a string "AB" or just four objects.

Consider this demonstrative program

#include <stdio.h>
#include <string.h>

int main( void )
{
    char character_array[4] = { 'A', 'B', '\0', 'C' };

    char *p = strchr(character_array, 'C');

    if (p == NULL)
    {
        printf("Character '%c' is not found in the array\n", 'C');
    }
    else
    {
        printf("Character '%c' is found in the array at position %zu\n",
            'C',
            (size_t)(p - character_array));
    }

    p = ( char * )memchr(character_array, 'C', sizeof(character_array));

    if (p == NULL)
    {
        printf("Character '%c' is not found in the array\n", 'C');
    }
    else
    {
        printf("Character '%c' is found in the array at position %zu\n",
            'C',
            (size_t)(p - character_array));
    }
}

The program output is

Character 'C' is not found in the array
Character 'C' is found in the array at position 3

In the first part of the program it is assumed that the array contains a string. The standard string function strchr just ignores all elements of the array after encountering the element with the value '\0'.

In the second part of the program it is assumed that the array contains a sequence of objects with the length of 4. The standard function memchr knows nothing about strings.

Conclusion.

This array

char character_array[4];

can contain 4 objects of type character. It is so declared.

The array can contain a string if to interpret its content as a string provided that at least one element of the array is equal to '\0'.

For example if to declare the array like

char character_array[4] = "A";

that is equivalent to

char character_array[4] = { 'A', '\0', '\0', '\0' };

then it may be said that the array contains the string "A" with the length equal to 1. On the other hand the array actually contain 4 object of type char as the second equivalent declaration shows.

Upvotes: 2

falkb
falkb

Reputation: 1349

You just reserve 4 bytes to fill with. If you write to _array[4] (the fifth character) you have a so called buffer overflow, means you write to non-reserved memory.

If you store a string in 4 characters, you have actually just 3 characters for printable characters (_array[0], ..., _array[2]) and the last one (_array[3]) is just for keeping the string termination '\0'.

For instance, in your case the function strlen() parses until such string termination '\0' and returns length=3.

Upvotes: 0

unwind
unwind

Reputation: 399753

Well, yes, you can store any four characters. The string-termination character '\0' is a character just like any other.

But you don't have to store strings, char is a small integer so you can do:

char character_array[] = { 1, 2, 3, 4 };

This uses all four elements, but doesn't store printable characters nor any termination; the result is not a C string.

If you want to store a string, you need to accommodate the terminator character of course, since C strings by definition always end with the termination character.

C does not have protection against buffer overflow, if you aim at your foot and pull the trigger it will, in general, happily blow it off for you. Some of us like this. :)

Upvotes: 5

Related Questions