Pointers are ints, zero is null, and strings are null terminated.

Pointers are ints, zero is null, and strings are null terminated.

Post by Jeff_Rel » Sun, 02 Jan 2005 22:00:19



Hi Kelsey, You imagined: <<
" null " isn't properly part of the C language at all
- and strings are not " null terminated " .

C uses the terms " NULL " and " null pointer constant ",
which are semantically a little more involved than
a simple character constant zero value,
with which strings are terminated. >>

Pointers are ints, zero is null, and strings are null terminated.
 
 
 

Pointers are ints, zero is null, and strings are null terminated.

Post by mlw » Mon, 03 Jan 2005 00:17:57


Pointers are not "ints." For the most part you can treat them as "unsigned
int" if you are careful.

NULL is typically defined as a void pointer to zero. A C string is a zero
terminated array of characters.

 
 
 

Pointers are ints, zero is null, and strings are null terminated.

Post by The Ghost » Thu, 06 Jan 2005 08:52:44

In comp.os.linux.advocacy, Jeff_Relf
< XXXX@XXXXX.COM >
wrote
on 1 Jan 2005 13:00:19 GMT
< XXXX@XXXXX.COM >:

Is this why there's a typedef/macro in the standard library,
intptr_t ?

There's also off_t, off64_t, and size_t.

Oh, and std::string values are not NUL-terminated
(NUL='\0'; NULL=(void *)0 ) unless one calls c_str().

--
#191, XXXX@XXXXX.COM
It's still legal to go .sigless.
 
 
 

Pointers are ints, zero is null, and strings are null terminated.

Post by Jeff_Rel » Thu, 06 Jan 2005 10:22:50


Hi Spooky, You mentioned: << std::string values are not NUL-terminated
( NUL = '\0' ; NULL = ( void * ) 0 ) unless one calls c_str(). >>

I never use std::string,
#define NULL 0 is defined in 33 .H files in MS C++'s standard include path.
I alway use 0, never the NULL macro, because I hate all-caps, for one thing.
 
 
 

Pointers are ints, zero is null, and strings are null terminated.

Post by Ray Ingle » Thu, 06 Jan 2005 22:13:54


Interestingly, an actual NULL pointer value is not guaranteed to be
"all bits zero". It's just supposed to be a bit pattern that can be
determined to be an invalid address. Assigning NULL to a pointer gets
that value, and I think assigning 0 via "=" is supposed to do it too.
Doing memset() on a struct containing pointers to set all the bits to
zero may not get pointers in the struct set to NULL, though.

Of course, in practice these days you'll search a long time before
you find a machine that doesn't use "all bits zero" as the NULL value.
But technically, according to the spec, that's not required.

--
Sincerely,

Ray Ingles (313) 227-2317

"Challenging programs for the 'gifted' are sometimes decried as
'elitism'. Why aren't intensive practice sessions for varsity
football, baseball, and basketball players and interschool
competition deemed elitism? After all, only the most gifted
athletes participate." - Carl Sagan
 
 
 

Pointers are ints, zero is null, and strings are null terminated.

Post by Linu » Fri, 07 Jan 2005 01:40:13

Ray Ingles poked his little head through the XP firewall and said:



True. In section 5.1.1 "Zero" of Bjarne's C++ book, it says (paraphrasing):

"Zero is an int. The standard conversions allow it to be used as a
constant of any integral, floating, or pointer type. Zero will typically
(but not necessarily) be all zero bits. Because of C++'s tighter type
checking, the use of plain 0 (rather than a NULL macro) leads to fewer
problems.

I like to follow advice from Bjarne.

--
Linux, because a CPU is a terrible thing to waste.
 
 
 

Pointers are ints, zero is null, and strings are null terminated.

Post by The Ghost » Fri, 07 Jan 2005 06:00:10

In comp.os.linux.advocacy, Jeff_Relf
< XXXX@XXXXX.COM >
wrote
on 5 Jan 2005 01:22:50 GMT
< XXXX@XXXXX.COM >:

Obviously.


void * p = 0;

is a perfectly valid C++ construct, so you're off the hook.

This time.

--
#191, XXXX@XXXXX.COM
It's still legal to go .sigless.
 
 
 

Pointers are ints, zero is null, and strings are null terminated.

Post by throop » Sat, 08 Jan 2005 05:53:56

: Jeff_Relf < XXXX@XXXXX.COM >
: Pointers are ints,

No. Pointers and ints may even have different sizes,
ie, 16 bit pointers and 32 bit ints, or vice versa, and
many other possibilities. See

http://www.yqcomputer.com/ ~scs/C-faq/q5.17.html

: zero is null,

No. NULL is zero (or zero cast to (void*)). See

http://www.yqcomputer.com/ ~scs/C-faq/q5.4.html

: and strings are null terminated.

No, strings are NUL terminated (and that's not a mispelling of NULL).
See

http://www.yqcomputer.com/ ~scs/C-faq/q5.13.html

And at the bottom of the ninth, Jeff is 0 for 3.


Wayne Throop XXXX@XXXXX.COM http://www.yqcomputer.com/
 
 
 

Pointers are ints, zero is null, and strings are null terminated.

Post by The Ghost » Sat, 08 Jan 2005 10:00:41

In comp.os.linux.advocacy, Wayne Throop
< XXXX@XXXXX.COM >
wrote
on Thu, 06 Jan 2005 20:53:56 GMT
< XXXX@XXXXX.COM >:

They are now. Pointers on 64-bit machines are 64-bit.
At least, one hopes so; there was a -taso option
at one point on the Alphas when building using OSF/1
(which is now named something else).

In times of yore, pointers might have been 4-byte while
ints are 2; then someone got the bright idea of making
the ints bigger as the hardware got more capable,
so pointers and ints became the same size again.

I'm old enough to remember when both were *2* bytes,
on a PDP 11/70 running Unix. Admittedly back then I
wasn't as sophisticated software-wise as I am today.
At some point I'm going to have to exhume a tape (it's
a 1/2" reel tape, too!) and see what my software really
looked like back in college in 1983... :-)


There used to be a Honeywell with *9-bit* characters.

I have worked on HP machinery (but not in C) that used
16-bits per word, word addressing. I think the Basic
interpreter divided by 2 to get the word of a string
byte it wanted, then took the requisite byte depending
on the odd bit out.

That line relating 8086es may have to be modified. At
some point many programs may have had both NEAR pointers
(16-bit offsets to the default Intel paragraph) and
FAR pointers (paragraph and offset). Also, I think there
were a number of memory models: tiny, small, medium, large,
and huge are the ones I remember. (This may have been
a Borlandism.)

tiny: everything's shoved into one segment
small: CS and DS use 16-bit offsets
medium: ?
large: ?
huge: far pointers everywhere?

Thank Intel the 386 allowed for 32-bit offsets. :-)
But what a pain.


It gets weirder, though. In C++, 0 is null:

void * p = 0;

is perfectly legal and accepted usage, AIUI.


Maybe 1 for 3, but it's an infield dribbler. (Uh,
should I rephrase that?)



--
#191, XXXX@XXXXX.COM
It's still legal to go .sigless.
 
 
 

Pointers are ints, zero is null, and strings are null terminated.

Post by Rich Gibb » Sat, 08 Jan 2005 13:37:13

The Ghost In The Machine said the following, on 01/06/05 20:00:
[snip]

Cray also had some machines that had unusual pointer representations.

[snip]

The construction is legal in C, too. The integer [literal] constant 0
is taken to be a null pointer when used in a pointer context. However,
this does _not_ mean that the internal representation of a null pointer
is the same as that of any integer type, or that it has all bits set to
zero. Nor are all pointers required to be the same size.

The 'void *' type is a generic object pointer. You can convert any
object pointer to/from 'void *' without losing information. This is
not, however, required to apply to function pointers.

--
Rich Gibbs
XXXX@XXXXX.COM