Merrill said:
[OP: Why is a char * a char and not an int]
"character constants are of type int and many functions
return int/have int parameters when we are dealing with
single characters -- why is it then that we store strings
in char arrays or memory pointed to by char * variables?"
Is that your question?
For that I have an answer
I would imagine that your answer is similar to K&R §5.5 .
> But since I don't get it, I would welcome the answer to
> that question.
It has been answered by others in this thread. So, here's
the short of it:
- 'a' is a character constant. A variable of type char can
hold a variable constant. The constant itself could also
be (in a non-portable way) expressed as a number in the
range [CHAR_MIN,...,CHAR_MAX]. Integer constants without
u/U, l/L or a combination are considered to be of type int.
- As characters and character constants can be (from the
C type point of view) most efficiently be stored in
char variables, we use char arrays to store more than one
of them instead of int arrays. If there is a value 0 in
this char array, we have a C string (terminated by 0, or
'\0' if you like that better).
- We only talk of "strings" if the array is a char array.
- Functions dealing with strings expect dealing with char
arrays or memory areas pointed to by char *.
Nils Selasdal:
Cause you, the programmer, made it a char pointer.
Which seems good, as you're cop[y]ing a C string to it,
and strings are usually best manipulated as sequences
of chars, it is also what "%s" in your printf statement
would expect..
K&R §2.2: "There are only a few basic data types in C:
^^^^^
> char, int, float, double."
Is char * a data type? How about int **? MPJ
Yes. 'Tis.
You seem to have problems wrapping your mind around the
concept of pointers. Is this right?
If no, are you comfortable with the relationship between
pointers and arrays? Else: What do you think an array is?
Do you understand why there is a difference between
passing a variable's value or its adress?
If your actual problem is with pointers, you should try
to pin down your difficulties, have a peek at the FAQ
(most people think along similar lines when it comes
to misunderstanding pointers), reread the chapter in K&R
and come back here to ask the questions left.
Please formulate them in a succinct but precise way (if
necessary, do a definition of terms first).
Cheers
Michael