inclusive-lower-bound, exclusive-upper-bound (was Re: Range Operation pre-PEP)

Greg Jorgensen greg at pdxperts.com
Sat May 19 05:03:23 EDT 2001


On 17 May 2001, Marcin 'Qrczak' Kowalczyk wrote:

> Let me be pedantic too.
>
> > * Pointers are modifiable l-values, array names are not.
>
> Pointers are not necessarily l-values. For example witn this declaration:
>     int i;
> &i is a pointer but not l-value.

&i is an expression that yields the address of i. That expression result
is not an lvalue, though the address can be assigned to a pointer object,
which is an lvalue.

I wasn't pedantic enough: I should have written "pointer object" instead
of pointer to distinguish between pointer objects and pointer
values/addresses.

> > * The address of an array is known at compile time, but the value of a
> > pointer is not known until run time.
>
> After
>     int (*a)[10], i;
> a[i] is an array whose address is not known at compile time.

But "a" isn't an array, it's a pointer to an array. And the definition of
"a"  doesn't actually allocate an array of anything, so there is no
physical address of an array for the compiler to know. According to cdecl
the declaration "int (*a)[10]" means "declare a as pointer to array 10 of
int."

> > * An array definition implicitly allocates memory for the array elements.
> > A pointer definition/declaration does not allocate any memory.
>
> It allocates memory for the pointer itself.

Yes, it allocates memory for the pointer object but not for anything the
pointer may point to.

Actually ANSI C does support a special case shorthand for character
arrays:

    char *s = "literal string";

will allocate space for the literal string and set s to point to the
string. Technically (pedantically) the appearance of the literal string
alone--regardless of its context--will allocate memory for the string. But
the shorthand definition above acts like a pointer definition that
allocates memory. char s[] = "literal string" is preferred unless s really
needs to change to point at something else at run time.

Greg Jorgensen
Portland, Oregon USA
gregj at pobox.com





More information about the Python-list mailing list