Java gets its notation of array from C, as well as the notion of a string. But the implementations and philosophies and programming standards are very different. A Java programmer will find this particularly painful.
In Java, String is a built-in class, and arrays are a special kind of Object.
C strings and arrays are just pointers.
notation
Both languages use the square bracket “[]” notation for arrays.
There is no string concatenation operator in C, only library functions that handle data that can be interpreted as a string.
C strings
- strings are pointers to
charconventionally terminated by'\0' - a string of n characters therefore occupies n + 1 bytes of memory.
charis an 8-bit byte.- C11 standard defines
char32_t, etc.; accommodates Unicode - standard library has lots of functions (see string.h)
C string literals
- The notation for C string literals is much like the Java analog
- C string literals are 7-bit ASCII (not Unicode).
- literals coded next to one another (e.g. on separate
lines without a semicolon (“
;”) are concatenated (convenient for long literals).
C arrays and pointer arithmetic
- the name of the array is just a pointer to the first element:
a == &a[0]is always true. a[i]means precisely*(a + i)(that’s pointer arithmetic).
C multi-dimensional arrays
- that the notation
a[i][j]suggests an array of arrays - bounds of multi-dimensional arrays must be compile-time constants (must be passed to subroutines)
- alternatively can construct a real array-of-arrays,
- stored so that elements that differ only in last subscript are adjacent in memory.
dangers
- No bounds checking in C: writing off
the end of an array or failing to terminate a string with
'\0'does not automatically stop the program, and usually results in strange behavior. - The standard library for strings is very low-level and crude. To do a lot of string manipulation, use a string module for convenience and robustness. But the vast bulk of C code uses these libraries directly, so it is necessary to become familiar with them.