Shhnwz.a wrote:
I am in confusion regarding jargons.
When it is technically correct to say.. String or Character Array.in c.
just give me your perspectives in this issue.
Well, in C the issue is highly clouded because its not done in the same
way in any other language, and the syntax doesn't lead to an obvious
disambiguation between the two.
A C language string is a sequence of characters, and is supposed to be
terminated by the first '\0' appearing at the character beyond the end
of the string. Thus, the length of the string is dependent on its
content (i.e., where you first find the \0').
An array in C is a sequence of base type elements with a given size,
but whose contents are arbitrary. So an array of characters may be 100
elements long with half of them being the value '\0'. I.e., the length
of the array is not dependent on its contents.
Now the confusion arises because C does not have any kind of special
container for where strings are stored. In fact, the only sensible way
of doing things in C is to store the string inside of a character
array. Sometimes this storage is implicit and sometimes its explicit.
The problem is that you can declare an array of characters, and store a
string in it, but then you refer to the variable name for the array of
characters and use it as if it were the string. So you see the
difference between the two has to be conceptual and semantic -- you
don't even have a seperation of syntax to help you.
So:
char a[100] = "Hello ";
void foo (char * b) {
char c[] = "world\n";
char * d;
d = b + strlen(b);
strcpy (d, c);
}
The variable a is an array of 100 characters which happens to hold the
string "Hello ". The c variable is an array whose length is 7 and
holds the string "world\n" -- this is just a special cases of C that
lets the length of arrays be determined by their initializer, and C
recognizing the "world\n" initiailizer as basically being { 'w', 'o',
'r', 'l', 'd', '\n', '\0' }. d is, of course, a pointer, which can
point inside of arrays, or any other valid storage. The parameter b is
a pointer to a character -- it is not enforced by the language to point
to a character array, nor is it required that the contents be a valid
string (it may in fact be NULL). The line d = b + strlen(b) interprets
b *as if* b were pointing to valid string contents, and computing the
pointer which points to one position past the end of the string b
(i.e., it should be pointing to a '\0'). Then the strcpy() call at the
end interprets both d and c as if they were strings, and will
implicitely end up using the tail of b's storage container to hold the
final concatenated result.
So whether or not a, b, or c is a string, depends on how you use them.
If at some point in the foo function, I had put c[2] = (char) rand();
then we might interpret that as using c in an array-like fashion
(rand() might output 0). If we invoke the expressions "sizeof (a)" or
"sizeof (c)" we would also see that these are array interpretations are
not affected by the length of the string content they may contain.
Learning all this nonsense is kind of a right of passage for becoming a
C programmer. It might give you an appreciation for one particular
twisted way of doing low-level string programming. What is often not
stated in all this, is the fact that this manifestation of strings is
both slow and error-prone. There is no reward for the pain of doing
things this way. Its just penance without purpose. But such is the
way things are in the C language.
If you have learnt strings from another programming language, then
chances are you will find my alternate implementation of strings for
the C language far more intuitive and sensible:
http://bstring.sf.net/
.. (However, if you are a beginner just learning C you still need to
learn about memory allocation semantics to use it without issue.) In
"The Better String Library" strings are their own type and are not
directly interchangable with character arrays, so the distinction
between the two is completely obvious.
--
Paul Hsieh
http://www.pobox.com/~qed/ http://bstring.sf.net/