Array Declarations
int a[5]; char s[30]; double a[5][7];
Array References
for (int i = 0; i < n; i++) { a[i] = i; }
Array Sizes
Prior to C99, sizes had to be constants. Now sizes can be variables.reverse.c
Array Initializers
int a[3] = { 1, 2, 3 }; int a[2][5] = { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10} } char s[3] = { 'a', 'b', 'c' }; char s[4] = "abc"; char *a[3] = { "foobar", "xyzzy", "plugh" };
Linear Search
/* lsearch - searches an array of strings for target. returns the position of the first occurrence of target or -1 if target does not occur. */ int lsearch(int n, char *a[n], char *target) { int i = 0; while (i < n && strcmp(target, a[i]) != 0) { i++; } if (i < n) return i; else return -1; }
Pointers
A pointer variable stores the address of a data item.
Pointer Declarations
int *p; char *s = "foobar"; double *x = &y;
Address and Dereference Operators
- *p dereferences p (gives the contents stored in the location to which p points). p must be a pointer.
- &x gives the address of x.
- * and & are inverse operations.
Pointer Example
int x = 3; int *y = &x; (*y)++;
This increments x!
Pointers vs Arrays
- The value of an array variable is a pointer to the first item in the array.
- If a is an array, then a[0] and *a are the same.
- Pointer arithmetic works in increments of the size of a data item, so that *(a+i) is the same as a[i].
Dynamic Allocation
int *p = malloc(sizeof(int));
allocates space for an int and makes p point to it.free(p)
frees memory that was previously allocated with malloc or calloc.int *a = calloc(n, sizeof(int));
allocates space for n ints and makes a point to it. Effectively a becomes an array of size n of ints.
Dynamic Arrays vs Variable Length Arrays
- Dynamic arrays are allocated with malloc or calloc on the heap and are available until freed or the program terminates.
-
Variable length arrays (
int a[n]
) are allocated on the run-time stack and go away when the declaration goes out of scope (when you are no longer within the enclosing { }).
Strings
- A string is an array of char. Conventionally, strings are terminated with null
character (one whose ASCII code is 0, denoted by
'\0'
). - The string "foobar" looks like this:
'f' 'o' 'o' 'b' 'a' 'r' '\0'
String Functions
#include <string.h>
strcmp(s1, s2)
compares two strings - return 0 for equal, < 0 for <, and > 0 for >.strcpy(s1, s2)
copies s2 to s1.strcat(s1, s2)
concatenates s2 onto the end of s1.
Manipulating Strings
- It's usually preferable to use pointers rather than array references.
- strcpy (version 1)
void strcpy(char s[], char t[]) { int i = 0; while (t[i] != '\0') { s[i] = t[i]; i++; } s[i] = '\0'; }
- strcpy (version 2)
void strcpy(char *s, char *t) { while (*t != '\0') { *s = *t; s++; t++; } *s = '\0'; }
- strcpy (version 3)
void strcpy(char *s, char *t) { while ((*s = *t) != '\0') { s++; t++; } }
- strcpy (version 4)
void strcpy(char *s, char *t) { while ((*s++ = *t++) != '\0'); }
- strcpy (version 5)
void strcpy(char *s, char *t) { while (*s++ = *t++); }
Reading Strings
gets(s);
Reads a string from stdin. NEVER USE THIS FUNCTION!fgets(s, size, stream)
Reads a string from stream. Reads at mose size-1 characters. Stops at EOF or newline. If newline is read, it is stored.scanf("%s", s)
Reads non-whitespace characters. If a width specifier is
given, reads at most that many characters. NEVER USE WITHOUT A WIDTH SPECIFIER!