Difference between "char *arr" and "char arr[]"

Chris Torek chris at mimsy.umd.edu
Mon Sep 24 16:03:02 AEST 1990


In article <ENAG.90Sep23231057 at hild.ifi.uio.no>
enag at ifi.uio.no (Erik Naggum) writes:
>Let me venture a concise explanation to this difference:
>
>	extern char *arr	declares an object, containing a
>				pointer to a character
>
>	extern char arr[]	declares a constant pointer to a
>				character

Concise, yes; correct, no.  Both declare objects; the latter declares
an object that is an array.  It IS an array; it is NOT a pointer.  The
confusion occurs because objects that are arrays are *converted into*
VALUES that are pointers, whenever the value is called for.

In other words, if `x' is an array, one might try to describe the
`value' of x as the values of all elements of x.  (Mathematically, `the
value of x' would be `the set of all values x[i] such that x[i]
exists.')  If `x' is a large array, that would be an awful lot of
values.  Most computers can only work on one or two or maybe a dozen
or a hundred values at a time, and x could easily contain several
thousand values.  So the C language does not provide a way to talk
about this kind of `value of x'.

Instead, when you ask for the `value' of x (where x is some array),
the language gives you a convenient place to start if you wanted to
go about finding all of the (possibly many thousands of) values x[i].
Indeed, it tells you where the very first such value is stored.  In
other words, it changes an

	<object, array 8000 of int, x>

into a

	<value, pointer to int, &x[0]>

and you can then go about fetching all 8000 values yourself, using
this pointer.

Since it changes the <object, array N of T> into a <value, pointer to T>,
the result is not something you can change---you cannot, after the fact,
change the place the system decided to store the array, at least not in
the C language proper (assembly language hacking is another matter)---and
this leads people to (incorrectly) decide that an array `is' a constant.
It is nothing of the sort: an array IS an array.  It is not something you
can change, but it is also not a constant, as the (nonportable) program

	#include <stdio.h>
	void f(v) int v; {
		int arr[1000];
		printf("%lx\n", (long)arr);
		if (v) f(0);
	}
	int main() { f(1); return 0; }

will demonstrate on those systems that do not remove the tail recursion.
(You will get two different numbers.)

(N.B.: By switching from one meaning to another, you *can* say that an
array `is a constant'.  In C, as in many languages, `constant' is a
language concept meaning `a value that never changes'.  But the
location of an array *can* change, at least if that array is
automatic.  You, as a programmer, may not change it; the system can.
If you decide that you want the word `constant' to mean `I, as a
programmer, may not change this', then of course the location is *that*
sort of `constant'.  But you will confuse everyone else---as I probably
just did with this whole aside.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 405 2750)
Domain:	chris at cs.umd.edu	Path:	uunet!mimsy!chris



More information about the Comp.lang.c mailing list