Recall Character Arrays
- We can create and initialize arrays of characters:
char hello[] = {'h', 'e', 'l', 'l', 'o', '!'};
- Common characters needing escapes:
\n a ``newline'' character
\b a backspace
\r a carriage return (without a line feed)
\' a single quote (e.g., in a character constant)
\" a double quote (e.g., in a string constant)
\\ a single backslash
- Character constants always use single quotes.
C Strings
- A string constant is zero or more characters enclosed in double
quotes.
printf("Hello world.\n");
- Strings in C are always terminated internally by a null
character ('\0').
char word[] = { "Hello!" };
char word[7] = "Hello!";
char word[] = "Hello!";
char word[] = { 'H', 'e', 'l', 'l', 'o', '!', '\0' };
printf("She said %s\n", word);
- Let's write code to concatenate two strings. Here is a skeleton that we can fill out.
Single quote for char constants, double quotes for strings. Must be null
terminated. What is a null?
Using strings in C
- Often you'll need to know how long a string is
- E.g., to see if a copy of it will fit into a destination buffer
- Use can call strlen(3c),
which returns the length of the string (i.e., the number of characters in it), not including the terminating null:
char string7[] = "abc";
int len = strlen(string7);
printf("%d\n", len);
Which might be implemented as
int mystrlen(char str[]) {
int i;
for(i = 0; str[i]; i++)
;
return i;
}
- And of course we can print strings using printf() with the %s format
(e.g., printf("%s\n", string5);).
Pointers Revisited
- With pointers, we can manipulate addresses or contents referenced by the addresses.
- We can first declare a pointer variable
int *ip;
Which tells the compiler that variable
ip is of type
(int *) or equivalently that
*ip is of type
int.
Pointers contain addresses (Note that ip is currently undefined).
We can set to the address of another variable easily, as in
int i = 5;
ip = &i;
At this point, the contents ip and the address of i are the same -- they both refer to the same memory location, which contains the number 5.
Pointer Declarations
- As with any other variable types, you can initialize the value of a pointer variable when you declare it, as in
int *ip = &i;
but you cannot initialize the value of the memory location to which it points,
as something like
int *ip = 5;
will only tell the compiler to use address 5 as the initial value for ip (and the contents of address 5 are undefined, and probably off-limits to your program anyway).
Pointer Declarations
- While the compiler thinks these are equivalent,
int *j;
int* j;
the latter leads to possible problems later, such as writing
int* i,j;
when you wanted two pointers.
Pointer arithmetic
- In addition to single variables, pointers can be used to access parts of an array.
int *ip;
int a[20];
ip = &a[3];
Given that ip points to element 3 of a, we can use pointer arithmetic to access elements before or after 3, as in
ip++;
*ip = 7;
*(ip+1) = 8;
*(ip-2) = 3;
which set element 4 to 7, element 5 to 8 and element 2 to 3.