Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are C strings guaranteed to be arrays?

Tags:

c++

c

string

Are C strings (as opposed to std::string) guaranteed to be implemented as arrays? For example, say, I have

char const * str = "abc";

What it boils down to is whether or not str + 4 a legal pointer value (without dereferencing that is). I'm asking this because I dont know if C strings are a special case due to the null character terminating it.

like image 973
Thomas Eding Avatar asked Dec 27 '22 03:12

Thomas Eding


2 Answers

First part of the question

Are C strings guaranteed to be implemented as arrays?

For example, say, I have: char const * str = "abc"

Yes, a string object is of an array type. A character string is a data format and a (character) string object is of a type array of char.

In your example str points to the string literal "abc". Character string literals have the type char[N+1] where N is the length of the string (i.e., the number of characters excluding the terminating null character).

Some references from Standard and K&R 2nd edition:

C defines a string literal as:

(C99, 6.4.5p2) "A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, as in "xyz"."

and says (emphasis mine):

C99, 6.4.5p5) "For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence;"

K&R 2nd edition says:

"Technically, a string constant is an array of characters"

and

"when a string constant like "hello\n" appears in a C program, it is stored as an array of characters containing the characters in the string and terminated with a '\0' to mark the end."

Second part of the question

What it boils down to is whether or not str + 4 a legal pointer value (without dereferencing that is).

Yes, it is a valid pointer. In your case str + 4 is a pointer one past the last element of the array.

A valid pointer is a pointer that is either a null pointer or a pointer to a valid object. For an element of an array object, a pointer one past the last element of the array object is also a valid pointer.

Note that for the purpose of the last rule ("the one past element"), for pointers to objects that are not elements of an array, C treats the object as an array of one element.

(C99, 6.5.6p7) "For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type."

like image 191
ouah Avatar answered Dec 30 '22 09:12

ouah


They are guaranteed to be a contiguous sequence of chars. If that's your definition of an array, then yes.

In your example you will have 4 chars, one for each character and one for the null terminator. str+4 will be out of range.

like image 40
Mark Ransom Avatar answered Dec 30 '22 10:12

Mark Ransom