Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > String and Character Array...

Reply
Thread Tools

String and Character Array...

 
 
Shhnwz.a
Guest
Posts: n/a
 
      12-05-2006
Hi,
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.
Thanx in Advance.

 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      12-05-2006
Shhnwz.a wrote:
> Hi,
> 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.
> Thanx in Advance.


In C, all strings are character arrays but the reverse is not always
true. For example:
char carray[BUFSIZ];
declares an array of BUFSIZ characters.
char string[] = "Hello";
declares an array of type char having six elements. In can also be
treated as a string as long as string[5] == '\0' is true.

If you do string[5] = 'o', then string becomes an array of char but is
no longer a string. It will invoke undefined behaviour if passed to
functions that expect a nul terminated string.

In short, a string is an array of character[s], terminated by a nul
character. A plain array of characters has no such requirement.

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      12-05-2006
"Shhnwz.a" <(E-Mail Removed)> writes:
> 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.


A character array is a type (or an object of that type). For example,
"char[10]" is a particular character array type; given "char
obj[10];", "obj" is an object of that type.

A string in C is not a data type; it's a data layout. As the standard
says:

A _string_ is a contiguous sequence of characters terminated by
and including the first null character.

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      12-05-2006
Shhnwz.a said:

> Hi,
> I am in confusion regarding jargons.
> When it is technically correct to say.. String or Character Array.in c.


An array is a cardboard box[1]. A string is something you might put in it.
You might put something else in there instead, of course - or the box might
just be full of junk - but it's kind of made for a string, just like
egg-boxes are kind of made for eggs.


[1] ...except that, nowadays, arrays are hardly ever made from cardboard.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
 
Reply With Quote
 
jaysome
Guest
Posts: n/a
 
      12-05-2006
On Tue, 05 Dec 2006 06:47:47 GMT, Keith Thompson <(E-Mail Removed)>
wrote:

>"Shhnwz.a" <(E-Mail Removed)> writes:
>> 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.

>
>A character array is a type (or an object of that type). For example,
>"char[10]" is a particular character array type; given "char
>obj[10];", "obj" is an object of that type.
>
>A string in C is not a data type; it's a data layout. As the standard
>says:
>
> A _string_ is a contiguous sequence of characters terminated by
> and including the first null character.


And here is an _object_ of a character array _type_ initialized to a
_string_:

char obj[10] = "Hello!";

--
jay

 
Reply With Quote
 
santosh
Guest
Posts: n/a
 
      12-05-2006
Keith Thompson wrote:
> "Shhnwz.a" <(E-Mail Removed)> writes:
> > 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.

>
> A character array is a type (or an object of that type). For example,
> "char[10]" is a particular character array type; given "char
> obj[10];", "obj" is an object of that type.


But it's not a first class type. Also dynamic arrays are an exception.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-05-2006
"santosh" <(E-Mail Removed)> writes:
> Keith Thompson wrote:
>> "Shhnwz.a" <(E-Mail Removed)> writes:
>> > 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.

>>
>> A character array is a type (or an object of that type). For example,
>> "char[10]" is a particular character array type; given "char
>> obj[10];", "obj" is an object of that type.

>
> But it's not a first class type.


That's probably true for any reasonable definition of "first class",
but it's not at all clear what that means. The language defines
different sets of operations for different types; array types just
happen to have relatively fewer defined operations than other types.

We can't add pointers, and we can't compare structures. Maybe
pointers are second class, structures are third class, and arrays are
fourth class?

> Also dynamic arrays are an exception.


Um, an exception to what?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      12-06-2006
jaysome wrote:
>
> On Tue, 05 Dec 2006 06:47:47 GMT, Keith Thompson <(E-Mail Removed)>
> wrote:
>
> >"Shhnwz.a" <(E-Mail Removed)> writes:
> >> 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.

> >
> >A character array is a type (or an object of that type).
> >For example,
> >"char[10]" is a particular character array type; given "char
> >obj[10];", "obj" is an object of that type.
> >
> >A string in C is not a data type;
> > it's a data layout. As the standard says:
> >
> > A _string_ is a contiguous sequence of characters terminated by
> > and including the first null character.

>
> And here is an _object_ of a character array _type_ initialized to a
> _string_:
>
> char obj[10] = "Hello!";


There's actually seven different strings contained
within that object.

strlen(obj)
strlen(obj + 6)

--
pete
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-06-2006
pete <(E-Mail Removed)> writes:
> jaysome wrote:

[...]
>> And here is an _object_ of a character array _type_ initialized to a
>> _string_:
>>
>> char obj[10] = "Hello!";

>
> There's actually seven different strings contained
> within that object.
>
> strlen(obj)
> strlen(obj + 6)


Very cute. But there are actually ten of them; you missed:

strlen(obj + 7)
strlen(obj +
strlen(obj + 9)

(The array elements not explicitly initialized are set to zero, i.e.,
'\0'.)

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
 
Reply With Quote
 
websnarf@gmail.com
Guest
Posts: n/a
 
      12-06-2006
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/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert string with control character in caret notation to realcontrol character string. Bart Vandewoestyne C Programming 8 09-25-2012 12:41 PM
FAQ 4.31 How can I split a [character] delimited string except when inside [character]? PerlFAQ Server Perl Misc 0 01-25-2011 05:00 AM
How can I replace all occurrences of a character with another character in std string? herman C++ 5 08-30-2007 09:05 AM
8 bit character string to 16 bit character string Brand Bogard C Programming 8 05-28-2006 05:05 PM
getting the character code of a character in a string Velvet ASP .Net 9 01-19-2006 09:27 PM



Advertisments