Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!mcvax!hp4nl!philmds!nlgvax!hans From: hans@nlgvax.UUCP (Hans Zuidam) Newsgroups: comp.sys.amiga Subject: Re: Struggling through the "C" Message-ID: <231@nlgvax.UUCP> Date: 7 Apr 89 11:44:06 GMT References: <2570@ssc-vax.UUCP> Reply-To: hans@nlgvax.UUCP (Hans Zuidam) Followup-To: comp.sys.amiga Organization: Philips Research Geldrop Lines: 60 >In article <97113@sun.Eng.Sun.COM>, Chuck McManis writes: >>char *a; /* This is a pointer to a string */ >>char a[80]; /* this is a pointer to a string with 80 bytes allocated */ In article <2570@ssc-vax.UUCP> dmg@ssc-vax.UUCP (David Geary) writes: > No! char a[80] is not a pointer at all. It is an array... Please check your K&R before responding: K&R 7.1, pg. 185: An identifier is a primary expression, provided it has been suitably declared as discussed below. If the type of the identifier is "array of ...", then the value of the identifier-expression is a pointer to the first object in the array, and the type of the expression is "pointer to ...". and K&R 7.1, pg. 186: The expression E1[E2] is identical (by definition) to *((E1)+(E2)). This is from: "The C programming language", Kernighan, Brian W., Ritchie, Dennis M. Prentice-Hall Software series, 1978. So: 'char *a' is *identical* to 'char a[80]' *except* for the fact that in the second definition 'a' is a *constant* pointer. That is: you cannot assign a value to 'a'. Ponder these: c = "ABCDEF"[i] a[i] and i[a] and *(a + i) /* The function */ f(a, b) char a[], b[]; /* Called by */ char *c, *d; f(c, d) And so on. A useful cosequence of this is that one can construct flexible arrays: int *p, i; p = (int *) malloc(NrOfElements * sizeof(int)); for (i = 0; i < NrOfElements; i++) p[i] =0; p = (int *) realloc(p, NewNrOfElements * sizeof(int)); /* and so on... */ Hans -- Hans Zuidam E-Mail: hans@pcg.philips.nl Philips Telecommunications and Data Systems, Tel: +31 40 892288 Project Centre Geldrop, Building XR Willem Alexanderlaan 7B, 5664 AN Geldrop The Netherlands