:: Re: [DNG] Making sense of C pointer…
Página Inicial
Delete this message
Reply to this message
Autor: Rainer Weikusat
Data:  
Para: dng
Assunto: Re: [DNG] Making sense of C pointer syntax.
karl@??? writes:
> Rainer Weikusat:
> ...
>> One thing to note here: Every C pointer is really a pointer to an array
>> of values, although the size of the array may just be one.
> ...
>
> I thought it was the other way around, a pointer is just an address to
> some (a single) memory location which can be part of an array, I'd
> not consider int ii to be an array even though int *pp = &ii,
> -- but who cares.


What I was trying to get at was that C doesn't differentiate between
'pointer to object' and 'pointer to array of objects': Assuming that p
is a valid pointer, the object pointed to by p can always be accessed
both via *p and via p[0] and p + 1 will always be a valid pointer, too,
either one pointing to the next element of the array if there are at
least two elements or the 'just beyond the end' pointer guaranteed to be
usable for size calculations via pointer subtractions.

This is also true if the pointer was implicitly created by using an
expression of array type for something other than

    "[...] the operand of the sizeof operator or the unary &
        operator, or [...] a string literal used to initialize an array"


Eg, after the following array definition

int a[] = {0, 1, 2};

*a is a valid expression for accessing the first element.

> But there is a real differnce between
> int arr[10];
> and
> int *pp = calloc(10, sizeof(int));
> as pp can be assigned to, but not arr, i.e. pp = malloc() works
> but not arr = malloc().


The pointer arr is converted to is not an lvalue.