Uh oh... I'm being thick again :-)

Started by
13 comments, last by Llamasoft.net 23 years, 11 months ago
Integers dont have to be zero terminated, to int Array[10] will give you 10 integers.

smack someone: if you do char Array[10], you can only use 0-8, with the 9th for the \0.

MENTAL

Happy birthday to me!
Advertisement
I''ll try and get this correct, because some people seem to be mixing stuff up.

First off - I''m talking C - not any other language.

int MyArray[10];

will give you an array of integers with ten elements, accessed via an array index, which starts at zero and ends at nine. i.e. the first element could be assigne as such:

MyArray[0] = 5;

and the last as

MyArray[9] = 10;

note that it is possible to overrun arrays, for example, given the array above, you could write:

MyArray[44] = 56;

The reason for this is that C performs no bounds checking on arrays, so the compiler may not find it illegal. However, it IS incorrect and will cause access violation errors, so don''t do it.

Now onto characters...

If you have an array declared like this:

char MyChars[10];

You have the same size array as above, elements zero to nine may be used, however, if you want the array to be treated as a string, then you will have to leave ONE element for the NULL terminator, which C uses to determine the end of a string (unless you want to do the work yourself, but thats another topic) this NULL terminator is the character ''\0''

So, given the array above, this is legel

strcpy(MyChars, "Programme");

because strcpy() will copy the 9 letter string specified, and automatically apply the NULL terminator.
this however, is not correct:

strcpy(MyChars, "Programmer");

because no element has been left for the NULL terminator.
You can, however, do something like this.

MyChars[0] = ''A'';
MyChars[1] = ''B'';
// carry on down....
MyChars[9] = ''J''

You will however, not be able to treat the array as a string, it is just some characters stored together with no special properties.

If I''m wrong, I''m sure someone will correct me.

-Mezz
This really isn''t hard to understand ppl, so im just gonna repeat what everyone said :

int array[10];

VALID INDICES: 0 - 9 (not 10 also, SmacknRat, unless you want to screw things up )

ACTUAL NUMBER OF INTEGER SLOTS: 10

Only strings need to have a null terminator ("\0"). The only way to make a valid string is to have the last character "\0" (in other words, if you''re gonna make a string, leave plenty of extra characters for whatever may happen ). Integers are happy little fellows that don''t need one AND CAN SURE AS HELL SURVIVE WITHOUT ''EM SO DON''T HARASS THOSE POOR INTEGERS!!!

*Zipster straigtens out his tie and fixes his hair again*

Ok, im ok now .

-----------------------------------------------------------
PCMCIA - People Can't Memorize Computer Industry Acronyms
ISDN - It Still Does Nothing
APPLE - Arrogance Produces Profit-Losing Entity
SCSI - System Can't See It
DOS - Defunct Operating System
BASIC - Bill's Attempt to Seize Industry Control
IBM - I Blame Microsoft
DEC - Do Expect Cuts
CD-ROM - Consumer Device, Rendered Obsolete in Months
OS/2 - Obsolete Soon, Too.
WWW - World Wide Wait
MACINTOSH - Most Applications Crash; If Not, The Operating System Hangs
quote:Original post by borngamer

As far as leaving a character for "\n", I''m pretty sure that only applies to strings, say you were doing something like.



Strings don''t exist in C/C++. They are not a primitive type. Howevere they seem to be quite useful, so someone have defined a "string" class ;-).

It is a mere convention to assume that a ''null'' character in an array of ''char'' ends the "string" itself. Any other non-printable symbol would be the same.

The "string" is just a sequence of etherogenous symbols, and that''s why they are usually modeled using an array.

If you were storing the string length you don''t need to put the null-terminating character. For example, you could store in the first slot of the array the "string" length.

In general a "string" can be built on any alphabet. So you can define "strings of integers", "strings of shorts", "strings of doubles", and so on...

Usually, we refer to "strings" in the textual meaning of the term. So it''s natural to model them as an array of ''char'' (for the ASCII alphabet) or ''short'' (for the wide-character set).

Hope to be useful.


Bye,

Karmalaa
So, you could have a "string" of

---[home page] [[email=karmalaa@inwind.it]e-mail[/email]]
Mezz got it right.

"What if that had been porn?"
-Friends
=============================="What if Bill Gates hade a penny everytime windows crashed? Oh wait, he does!"-Dont know who said that, but it's funny :)-Joacim Jacobsson

This topic is closed to new replies.

Advertisement