This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: pointers &arrays[]


>In a recent chat room discussion, pointer notation of arrays in C was
>brought up.  The question is, are array names pointers?  Is array
>subscripting another form of pointer notation, or visa-versa?  I realize
>the pointer notation works for everything except sizeof() (and maybe
>others).  So that

>array == &array[0]
>array == &array
>*array == array[0]
>*(array+n) == array[n]

>are all true given array[n].  In this case, sizeof(array) returns the size
>of the entire array, not array[0].  Are there other examples where this
>pointer notation fails?
>Lastly, is this pointer notation implementation dependent or is it part of
>the de facto standard?

dear oh dear. the pointer notation is not failing. it's perfectly well-defined.
and it's probably part of the real-honest-to-goodness-standard. it's just that
it's very stupid and you have to pay close attention.

anyway, here goes:

--------------------------------------------------------
--- THERE'S NO SUCH THING AS AN ARRAY IN A C PROGRAM ---
--------------------------------------------------------

array names are names, not pointers but they might as well be. this is what
happens. c parsers convert array names into the address of their first
element (i.e. this happens before actual compilation).

	array --> &array[0]

this is a constant pointer.

c parsers convert array indices with the appropriate pointer arithmetic
(i.e. this happens before actual compilation).

	array[index] --> *(array + index)

the reason for this is that there is no such thing as an array in a c program.
there is only array *syntax*. pay careful attention here. syntax does not imply
underlying corresponding objects. c's array syntax is only syntactic sugar for
pointers. it does not imply any such entity/object as an array.

this is why sizeof(array) usually returns 4 (the size of a pointer):

	sizeof(array) == sizeof(&array[0]) == sizeof(any pointer) == 4

however, the syntactic sugar is very sweet. sizeof(array) *will* give you
the total size of the contents of the array *iff* the size is known at compile
time (because sizeof is a compile time expression).

e.g.

	sizeof("string literal")

is treated like a constant expression that can and is evaluated by the parser
or compiler (returns 15).

	char blerk[] = { 1, 2, 3, 4, 5 };
	sizeof(blerk)

will return 5

	char blerk[3] = { 1, 2, 3 };
	sizeof(blerk)

will return 3

this only applies to declarations in a function body or outside a function,
but *not* in function argument lists.

	int func(char arg[32])
	{
		printf("%d\n", sizeof(arg));
	}

will print 4.

this is not constant pointer like in a file/function level array declaration,
it is just a pointer (and, no, sticking a "const" in front won't change that:).

even though it *looks* like an array of 32 bytes, it's only a parameter
declaration which doesn't count. parameter declarations refer to the stack,
not the data segment. so it's just a 4 byte pointer. parameters are not
compile time entities, they are run time entities.


raf

ford2
-
For help on using this list (especially unsubscribing), send a message to
"gnu-win32-request@cygnus.com" with one line of text: "help".


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]