user9311878
user9311878

Reputation:

End of array in C language

We know that for character arrays we have the '\0' to identify its end, what about other types of arrays?

I presume it's not a special character such as the '\0' since to declare an integer array of n elements for example you just allocate n * sizeof(int), yet for a character array we account for the '\0'.

Upvotes: 11

Views: 39964

Answers (4)

user3586395
user3586395

Reputation: 21

You have in C the possibility to declare a string in different ways (actual string type does not exists in C):

char MyStr[5] = "Jack"; 
char * MyStr  = "Jack"; 
char MyStr[5] = {'J','a', 'c', 'k', '\0'};

In case 1 and 2, when compiler encounters " " it will appends a null character '\0' at the end . In the last case you should append it manually (by convention you use '\0') but the main point is to not access elements outside the array.

Example :

while (*Mystr) /* Check against NULL char*/
{
  /* Do something */
}

If you use pointer to character type you cannot change it anymore after declaration because it will be stored in read only segment.

Upvotes: 0

klutt
klutt

Reputation: 31296

A character array is just an array of type char[]. It has nothing that identifies it's end. What you are talking about is a c-string, which is a null-terminated char array. Or to be more correct, a null-terminated contiguous sequence of characters. Technically, it does not need to be an array.

Consider this code:

char * p = malloc(20);
strcpy(p, "Hello, World!");

The pointer p will now point at a c-string that is not an array.

A char array does not need to be terminated with \0, but if it is, it is a c-string.

C standard 7.1.1.1

A string is a contiguous sequence of characters terminated by and including the first null character. The term multibyte string is sometimes used instead to emphasize special processing given to multibyte characters contained in the string or to avoid confusion with a wide string. A pointer to a string is a pointer to its initial (lowest addressed) character. The length of a string is the number of bytes preceding the null character and the value of a string is the sequence of the values of the contained characters, in order.

So in short, arrays does not have a terminator at all. Not even character arrays. The closest thing is sizeof which will give you the space allocated for the array, but it will not work the same way as a null-terminator. If you have allocated space dynamically, or are using a pointer to access the array, you cannot even use sizeof. Well, you can, but you will not get the size you want.

Upvotes: 2

Sami Hult
Sami Hult

Reputation: 3082

The reason that strings are null-terminated is for convenience. It's a convention adopted by a large number of stock procedures for string handling.

You can handle strings without null-termination, if you keep record of their lengths. In the same way you need to keep record of the length of your arrays, unless you want to terminate them with a sentinel value - but don't expect any ready-made functions to understand that.

Upvotes: 0

NPE
NPE

Reputation: 500167

C arrays don't have an end marker.

It is your responsibility as the programmer to keep track of the allocated size of the array to make sure you don't try to access element outside the allocated size.

If you do access an element outside the allocated size, the result is undefined behaviour.

Upvotes: 9

Related Questions