473,581 Members | 2,908 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

String and Character Array...

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.

Dec 5 '06 #1
14 4055
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.

Dec 5 '06 #2
"Shhnwz.a" <ms***********@ gmail.comwrites :
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_Keit h) ks***@mib.org <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.
Dec 5 '06 #3
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.
Dec 5 '06 #4
On Tue, 05 Dec 2006 06:47:47 GMT, Keith Thompson <ks***@mib.or g>
wrote:
>"Shhnwz.a" <ms***********@ gmail.comwrites :
>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

Dec 5 '06 #5
Keith Thompson wrote:
"Shhnwz.a" <ms***********@ gmail.comwrites :
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.

Dec 5 '06 #6
"santosh" <sa*********@gm ail.comwrites:
Keith Thompson wrote:
>"Shhnwz.a" <ms***********@ gmail.comwrites :
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_Keit h) ks***@mib.org <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.
Dec 5 '06 #7
jaysome wrote:
>
On Tue, 05 Dec 2006 06:47:47 GMT, Keith Thompson <ks***@mib.or g>
wrote:
"Shhnwz.a" <ms***********@ gmail.comwrites :
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
Dec 6 '06 #8
pete <pf*****@mindsp ring.comwrites:
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 + 8)
strlen(obj + 9)

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

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <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.
Dec 6 '06 #9
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/

Dec 6 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2729
by: Tom Warren | last post by:
I found a c program called similcmp on the net and converted it to vba if anybody wants it. I'll post the technical research on it if there is any call for it. It looks like it could be a useful tool for breaking ties when a phonic call returns a bunch of possibilities. Also, I'm looking for someone that has a zip code file with alternate...
16
17395
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char * is 4 bytes, should the following yield 4, 5, of something else? (And, if something else, what determines the result?) char x = "abcd"; printf(...
7
7065
by: herrcho | last post by:
i'm in the course of learning C, and found these two words "string, string literal" confusing me.. I'd like to know the difference between them.. Thank you
14
12905
by: Charles L | last post by:
I don't know if this is a stupid quesiton or not. I would like to know how to convert an array of characters generated from a previous operation to a string ie how do I append a null character at the end? I haven't been able to do it so far. Is there a string function I can use? Can anyone help? Charles L
17
4648
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some analysis and I'm led to believe (but can't yet quantitatively establish as fact) that the two basic culprits are a lot of calls to: 1.) if(...
7
373
by: junw2000 | last post by:
In the following code: std::string s1; s1 = "ABCDE"; std::cout<<s1<<std::endl; s1 = '\0'; //LINE1 std::cout<<s1<<std::endl; //LINE2 At LINE1, I add a NULL terminator at s1, so s1 should end at s1, right?
9
1736
by: somenath | last post by:
Hi All, I was going through one piece of code which is written by an experience programmer and it is working fine. But I think the use of "strstr" is not proper because it may show undefined behavior. char *returnValueFromIniFile(char *iniFilePath,char *keyIntheFile)
10
18619
by: Lonifasiko | last post by:
Hi, Just want to replace character at index 1 of a string with another character. Just want to replace character at that position. I thought Replace method would be overloaded with an index parameter with which you can write wanted character at that position. But no, Replace method only allows replacing one known character with another. The...
19
5319
by: est | last post by:
From python manual str( ) Return a string containing a nicely printable representation of an object. For strings, this returns the string itself. The difference with repr(object) is that str(object) does not always attempt to return a string that is acceptable to eval(); its goal is to return a printable string. If no argument is given,...
0
7808
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8157
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8312
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5683
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3835
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1410
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.