Thursday, August 2, 2007

character and strings

Section 8. Characters and Strings

8.1: Why doesn't

strcat(string, '!');

work?

A: There is a very real difference between characters and strings,
and strcat() concatenates *strings*.

Characters in C are represented by small integers corresponding
to their character set values (see also question 8.6 below).
Strings are represented by arrays of characters; you usually
manipulate a pointer to the first character of the array. It is
never correct to use one when the other is expected. To append
a ! to a string, use

strcat(string, "!");

See also questions 1.32, 7.2, and 16.6.

References: CT&P Sec. 1.5 pp. 9-10.

8.2: I'm checking a string to see if it matches a particular value.
Why isn't this code working?

char *string;
...
if(string == "value") {
/* string matches "value" */
...
}

A: Strings in C are represented as arrays of characters, and C
never manipulates (assigns, compares, etc.) arrays as a whole.
The == operator in the code fragment above compares two pointers
-- the value of the pointer variable string and a pointer to the
string literal "value" -- to see if they are equal, that is, if
they point to the same place. They probably don't, so the
comparison never succeeds.

To compare two strings, you generally use the library function
strcmp():

if(strcmp(string, "value") == 0) {
/* string matches "value" */
...
}

8.3: If I can say

char a[] = "Hello, world!";

why can't I say

char a[14];
a = "Hello, world!";

A: Strings are arrays, and you can't assign arrays directly. Use
strcpy() instead:

strcpy(a, "Hello, world!");

See also questions 1.32, 4.2, and 7.2.

8.6: How can I get the numeric (character set) value corresponding to
a character, or vice versa?

A: In C, characters are represented by small integers corresponding
to their values (in the machine's character set), so you don't
need a conversion routine: if you have the character, you have
its value.

8.9: I think something's wrong with my compiler: I just noticed that
sizeof('a') is 2, not 1 (i.e. not sizeof(char)).

A: Perhaps surprisingly, character constants in C are of type int,
so sizeof('a') is sizeof(int) (though it's different in C++).
See also question 7.8.

References: ANSI Sec. 3.1.3.4; ISO Sec. 6.1.3.4; H&S Sec. 2.7.3
p. 29.

No comments: