From: raf AT comdyn DOT com DOT au (raf) Subject: Re: pointers &arrays[] 24 Nov 1997 22:15:05 -0800 Message-ID: <199711240834.TAA15696.cygnus.gnu-win32@mali.cd.comdyn.com.au> To: gnu-win32 AT cygnus DOT com Cc: fdc AT comdyn DOT com DOT au >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 AT cygnus DOT com" with one line of text: "help".