Array Initialization Array declarations can specify initializations for the elements of the array: int primes[10] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 ; initializes primes[0] to 2, primes[1] to 3, primes[2] to 5, etc. Initialization is permitted (in ANSI C) for arrays of both static and auto storage classes. Array initializers must be compile-time constants. If not enough initial values are specified, the remainder are initialized to zero.
Arrays and Pointers Arrays and pointer variables have similar properties: int a[10], *p, x; p = a; x = p[3]; a[3] = x; There is an important difference, though: pointer variables are variables, arrays are constants. Thus, a = p; /* ILLEGAL! */
Array Arguments Arrays can be passed as actual parameters to functions. corresponding formal parameters are pointer variables. The #include <stdio.h> int main(void) { int i; char *a[] = { "The", "quick", "brown", "fox", "jumped" ; void print(int i, char *x[]); for(i = 0; i < sizeof(a)/sizeof(char *); i++) print(i, a); printf("\n"); void print(int i, char **x) { printf("%s ", x[i]);
An equivalent way of declaring the print function is as follows: void print(int i, char *x[]) { printf("%s ", x[i]);
Multidimensional Arrays C (like Java) supports two- and higher-dimensional arrays. A two-dimensional array is just an array of arrays. int a[10][20]; The array a is stored in a contiguous region of memory, in row major order. &a[0][0] == a ( decay to pointer convention) &a[i][0] == a[i] ( decay to pointer convention) a and a[i] are constants. A three-dimensional array is an array of two-dimensional arrays, a four-dimensional array is an array of three-dimensional arrays, and so on.
Arrays of Pointers Though C syntax makes them appear similar, a one-dimensional array of pointers is a different sort of thing than a two-dimensional array. char a[10][20]; /* A two-dimensional array */ char *p[10]; /* An array of (uninitialized) pointers */ The quantities a[0], a[1],..., a[9] are constants, which require no initialization. The quantities p[0], p[1],..., p[9] are variables, which must be initialized before use. a occupies 10*20*sizeof(char) bytes of storage. p occupies 10*sizeof(char *) bytes of storage.
In both cases, C allows two subscripts to be supplied: a[3][12] = 0; p[3][12] = 0; but the latter is only meaningful if p[3] has been initialized to point to a char[] array with at least 13 entries.
Strings In C, strings are represented as arrays of characters. The end of a string is indicated by the null character ( \0 ). The number of elements of the array used to hold a string must be at least one more than the length of the string. String literals (e.g. "Hello world!\n") are actually pointers to a static array that is initialized at the beginning of program execution. String manipulation generally involves extensive use of pointers.
A Common Mistake A common mistake is to try to input a string without previously having allocated storage: /* Maximum length of an input line */ #define MAXLINE 132 int main(void) { char *str; printf("enter your name: "); scanf("%s", str); /* ERROR! */ printf("hello there, %s!\n", str); The pointer variable str is uninitialized, and no storage has been allocated to hold the input string!
String Library Functions The C library provides a number of library functions for manipulating strings: int strlen(char *s); Returns the length of the string s. int strcmp(char *s1, char *s2); Compares s1 to s2, and returns negative (less), 0 (equal), or positive (greater), based on lexicographic ordering. char *strcpy(char *dst, char *src); Copies string src into area pointed to by dst (must be large enough!).
char *strcat(char *dst, char *src); Concatenates string src with string dst, leaving the result in area pointed to by dst (must be large enough!). char *strchr(char *s, int c); Finds the first occurrence of c in the string s. pointer to the located character, if any, else NULL. Returns a To use these, #include <string.h>.
String Manipulation using Pointers The manipulation of strings generally makes heavy use of pointer variables. Often these are used to march through a string from beginning to end. /* * Determine the length of a string */ int length(char *s) { int n = 0; while(*s++!= \0 ) n++; return n;
/* * Convert a string into lower-case */ void lower_caseify(char *s) { for( ; *s!= \0 ; s++) { if(isupper(*s)) *s = tolower(*s);
/* * Reverse a string in place */ void reverse(char *s) { char *e, c; for(e = s; *e!= \0 ; e++) /* skip */; e--; for( ; s < e; s++, e--) { c = *e; *e = *s; *s = c;
Arguments to main() The main() function can be declared as follows: int main(int argc, char *argv[]); argc is the argument count, which is always 1. argv is an array of pointers to strings, which are the actual arguments. argv[0] is (by convention) the name of the program being run. argv[1], argv[2],..., argv[argc-1] are the command-line arguments (as would be available in Java String[] args argument to main). argv[argc] is NULL.
For example, if you invoke an executable C program as follows: $ myprog myarg1 myarg2 myarg3 then: argc will be 4, argv[0] will be "myprog", argv[1] will be "myarg1", argv[2] will be "myarg2", argv[3] will be "myarg3", and argv[4] will be NULL.