'\0' like in C

R

robertospara

Question to Tad McLellan:
Is there something in Perl like '\0' in C????????

U seams to be smart????????
So??????????????????????????????
Chill out guys :)
 
R

robertospara

But for me '\0' is the mark of the end of the array (or string we can
say when the type is char).
So when I have >>>$string = "abcd"<<< is there somewhere '\0' in the
end???????????????????
Ex.
char a[10] = "abcd";
and then we have
a[0] = 'a' a[1]='b' a[2]='c' a[3]='d'
a[4]='\0'<<<<<<<<<<<<<<<<<<<
a[4]='\0'<<<<<<<<<<<<<<<<<<<
a[4]='\0'<<<<<<<<<<<<<<<<<<<
a[4]='\0'<<<<<<<<<<<<<<<<<<<
So this is what I am talking about.
Greetings to all:)
Jürgen Exner napisal(a):
 
M

Mirco Wahab

robertospara said:
Question to Tad McLellan:
Is there something in Perl like '\0' in C????????

No.

No if you mean '\0' as a "signal"
for string library functions.

This is mainly because a "string" (scalar) "$text"
in perl isn't the address of the character array
(as in C) but its a pointer to a structure which
contains (among others) the address of this
character array in question.

In C, you have a "string" concept that uses
an extra character (\0) at the end of the data,
in Perl you have an extra integer at another
place in the said struct which says how long
it is.

Regards

Mirco
 
J

Jürgen Exner

[Please do not top post, trying to repair]
[Please do not blindly fullquote]
Jürgen Exner napisal(a):

But for me '\0' is the mark of the end of the array

In Perl the 'end' of an array is managed internally and is accessible to the
programmer in two different ways:
- $#array indicates the last used index in the array @array
- scalar(@array) indicates the number of elements in the array (usually
$#array+1 unless someone messed around with the start index of the array)
(or string we can say when the type is char).

An array of char has nothing to do with a string. Those are two totally
disjunct and unrelated data structures.
So when I have >>>$string = "abcd"<<< is there somewhere '\0' in the
end?

No, why should there be? Opposite to C in Perl that horrible crutch is not
needed. If you need the length of an array then use one of the two methods
mentioned above. If you need the length of a string then just use the
length() function.
Note: of course the first has nothing, nothing at all, to do with the
second.
??????????????????

Would you mind fixing your keyboard?
Ex.
char a[10] = "abcd";
and then we have
a[0] = 'a' a[1]='b' a[2]='c' a[3]='d'
a[4]='\0'<<<<<<<<<<<<<<<<<<<
a[4]='\0'<<<<<<<<<<<<<<<<<<<
a[4]='\0'<<<<<<<<<<<<<<<<<<<
a[4]='\0'<<<<<<<<<<<<<<<<<<<
So this is what I am talking about.

Yes, it is amazing how primitive C is when it comes to data structures. It
doesn't even know about strings and forces the programmer not only to use an
array of characters instead, but to manually(!) manage even the length of
the string. And if you are not very careful you can screw up really badly,
e.g. char a[10] = "abcdabcdabcdabcd";

Luckily Perl is far advanced in this regard:
$a = 'abcd';
is all you need to declare and define a string that contains the first 4
latin letters. And if you want to enlarge the string, then just do so:
$a = $a . 'abcdabcsabcdabcd'.

There may or may not be an array @a, too, but it would be totally unrelated
to the scalar $a.

jue
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top