Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Character Array vs String

Reply
Thread Tools

Character Array vs String

 
 
Sheky
Guest
Posts: n/a
 
      11-09-2011
Could anybody please mention difference between character array and
string in C?
 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      11-09-2011
On Nov 9, 1:09*pm, Sheky <(E-Mail Removed)> wrote:
> Could anybody please mention difference between character array and
> string in C?
>

There's not much difference. In C, a string is an array of characters
with a terminating 0, or null character.

There's special syntax for specifying a string, which is the
conventional quotes. "Fred" creates a constant character array, 5
characters long, with a terminal null. We call that a "strign
literal". char name[5] = {'F', 'r', 'e', 'd', '\0'}; does exactly the
same thing, but the array is writeable.

Occasionally you might need a character array without a terminating
null. This wouldn't be a string in C terms, and will probably cause a
crash if you feed it to a string function like strcpy(). Usually it's
better to put on the null even if you don't use it, because it's only
one byte, and it means you can print the string out with no trouble if
you need to do so for some reason. The other situation is where you
have several strings in the same array, with nulls between them. name
= e.g. char *name = "Fred\0Bloggs\0\0". A few Microsoft Windows
functions like to receive lists of strings like this. They use a
sequence of two nulls to indicate the end of the list.

--
Visit my website. Nice program to demonstrate entropy and the
hydrophobic effect.
http://www,malcolmmclean.site11.com/www

 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      11-09-2011
On 11/09/2011 06:09 AM, Sheky wrote:
> Could anybody please mention difference between character array and
> string in C?


A character array is an array of objects of character type, either char,
signed char, unsigned char, or wchar_t. It remains an array, regardless
of what's stored in those objects.

A string is a data structure that could, among other things, be stored
in a character array. "A string is a contiguous sequence of characters
terminated by and including the first null character." (7.1.1p1).

Example:

char array[] = "One\0Two";

Every single character in that array can be treated as the first
character of a different string; most of those strings overlap each
other. For instance, array+3 points at the string "", which is empty
except for the terminating null character. array+5 points at the string
"wo",

--
James Kuyper
 
Reply With Quote
 
Quentin Carbonneaux
Guest
Posts: n/a
 
      11-09-2011
On 2011-11-09, Sheky <(E-Mail Removed)> wrote:
> Could anybody please mention difference between character array and
> string in C?


To my knowledge, C does not have strings.

--
qcar
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-09-2011
On 11/9/2011 6:09 AM, Sheky wrote:
> Could anybody please mention difference between character array and
> string in C?


A character array is an array whose individual elements are
characters: `char this[42]', for example. (The term "character"
is ambiguous, since C has four types that could claim the name:
`char', `signed char', `unsigned char', and `wchar_t'. In informal
use "character" usually means `char', but be aware that the term
is not quite so specific and misunderstanding may occur.) Anyhow:
A character array is an array of "character" elements, just as an
`int' array is an array of `int' elements.

A string is a particular data structure that can be stored in
a character array. It consists of some sequence of "payload"
characters plus a special "sentinel" character to mark the end of
the string. The sentinel character has the numeric value zero, and
no payload character has that value. It is possible to have no payload
characters at all (the sentinel will be in the array's first position),
and this represents the empty string. (Note: The terms "payload" and
"sentinel" are not formally defined by C; they're just my attempt to
name and explain the parts of a string.)

Example:

char message[10];
strcpy(message, "Hello");

`message' is a character array: A ten-element array where each element
is a character. After strcpy(), a string of six characters (five
payload and one sentinel) inhabits the first six positions of the
array. The remaining four characters are still part of the array,
but are not part of the string.


<---------------------- array ------------------------------>

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
message: | 'H' | 'e' | 'l' | 'l' | 'o' | 0 | ? | ? | ? | ? |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
sen-
<---------- payload ----------> ti-
nel

<------------- string ------------->

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      11-09-2011
For unicode support in C is documented in C99. In C89 the 8-bit ANSI char string was assumed and the termination of a string is a 0 stored in the of a string.

In some encoding C-code compiled to deal with ANSI C string might fail.
This was what I tested long time ago.

In programming languages length of a string might be stored explicitly in 2 to 4 bytes. Be careful of the length of two bytes stored that will cause trouble.

I think that the wchar that could support unicode can enhance the PC platform over the unix-lynux platform that might not support unicode in non-English users.

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-09-2011
On 11/09/2011 07:01 AM, Quentin Carbonneaux wrote:
> On 2011-11-09, Sheky <(E-Mail Removed)> wrote:
>> Could anybody please mention difference between character array and
>> string in C?

>
> To my knowledge, C does not have strings.


See 7.1.1p1, which I cited in my own response to Quentin.

C doesn't have a string type, but that's a very different question.
--
James Kuyper
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      11-09-2011
On Nov 9, 2:01*pm, Quentin Carbonneaux <(E-Mail Removed)> wrote:
> On 2011-11-09, Sheky <(E-Mail Removed)> wrote:
>
> > Could anybody please mention difference between character array and
> > string in C?

>
> To my knowledge, C does not have strings.
>

It has syntactic support for strings, as string literals. It also has
functions in the standard library that operate on strings. What it
doesn't have is a string type variable. Strings are passed about as
character pointers.
--
MiniBasic - a simple Basic interpreter with good string support
http://www.malcolmmclean.site11.com/www


 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      11-09-2011


"Sheky" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Could anybody please mention difference between character array and
> string in C?


As I understand it, a character array can contain a string, amongst other
things.

So in a ten-character array, if the first three characters are 'A', 'B' and
'C', and the fourth character is zero, then it contains the 3-character
string "ABC" at the beginning (and the other 6 characters can contain
anything).

But the more you look into this, the more complicated it gets (so that same
array also contains the string "BC", and the last 5 characters form a
5-character array, which may or may not contain a string of it's own, and so
on). So don't worry about it too deeply.

--
Bartc


 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      11-09-2011
CONTEXT!!
Leave some in so we know what you are responding to.

On Nov 9, 12:48*pm, 88888 Dihedral <(E-Mail Removed)>
wrote:

> For unicode *support in C is documented in C99. In C89 the 8-bit ANSI char


the character code you are trying to refer to is "ASCII" not "ANSI"

> string was assumed


no it wasn't. K&R (pre-standard C) may have done this (but I'm not
convinced even K&R was locked to ASCII). C89 went to a little trouble
to make it char set independent. No reason why a conforming C89
implementaions could not use EBCDIC (a rather nasty IBM character
code). And such things exist.

> and the termination of a string *is a 0 stored [at] the [end] of a string.


yes, the standard insists on this

> In some encoding C-code compiled to deal with ANSI C string might fail.


no. All conforming implementations must use zero to terminate a
string.
Badly written C programs might assume a particular character encoding.
But it isn't hard to write programs that are character encoding
neutral.

> This was what I tested long time ago.


anything that failed was either not a C compiler or your test progarm
was broken.

> In


--some--

> programming languages length of a string might be stored explicitly in 2 to 4 bytes. Be careful of the length of two bytes stored that will cause trouble.


Pascal did this typically. C++ std::string probably does it. There are
a zillion string libraries out there that do it. They will only cause
problems if you pass a non-C string to code taht is expecting a C-
string.

> I think that the wchar that could support unicode can enhance the PC platform over the unix-lynux platform that might *not support unicode in non-English users.


I'm pretty sure all main-stream OS's (Win, Linux, MacOS) support
unicode already. Hence no enhancement necessary.


 
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