Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!tut.cis.ohio-state.edu!cs.utexas.edu!uunet!cbmvax!kevin From: kevin@cbmvax.UUCP (Kevin Klop) Newsgroups: comp.sys.amiga Subject: Re: Struggling through the "C" Message-ID: <6597@cbmvax.UUCP> Date: 12 Apr 89 20:38:39 GMT References: <2570@ssc-vax.UUCP> <231@nlgvax.UUCP> <6542@cbmvax.UUCP> <17491@cisunx.UUCP> Reply-To: kevin@cbmvax.UUCP (Kevin Klop) Organization: Commodore Technology, West Chester, PA Lines: 83 In article <17491@cisunx.UUCP> ejkst@unix.cis.pittsburgh.edu (Eric J. Kennedy) writes: >It has been my understanding that "char *a;" gives you a pointer, >(pointing to some arbitrary place), while "char a[80];" actually >allocates 80 bytes of memory, and gives you a pointer to it, one that >you can't (or shouldn't) change. > >Thus, > >char a[80]; >main() >{ > strcpy(a,"hello"); >} > >is okay, while > >char *a; >main() >{ > strcpy(a,"hello"); >} > >will cause real problems, like overwriting your program or another task >with the word "hello". You'd have to do a malloc() or AllocMem() or >something to have a safe place to put the "hello". > >So, is this right, or is my feeling of confusion justified? > >-- >Eric Kennedy >ejkst@cisunx.UUCP You are correct in your statements. However, the I was addressing the common practice of thinking of the symbol 'a' in the following code: char a[80] as a pointer to the array, and using it as such. While this works most of the time, it is not 100% correct. For example, in most instances, the following code will have the same effect: function1() { char array[80]; char *a; a = array; strcpy (a,"Hello"); } and function2() { char array[80]; strcpy(array,"Hello"); } In both instances, "hello" will be placed into array[]; However, look at the definition of strcpy. It requires a char * for both its arguments. the identifier 'array' is not truly a char *, but can be used as such in this instance. This common practice is fine normally, but there are a few times that it can bite you. I think (but don't know for sure), that the technical definition of "array" in the above examples is really "&array[0]". -- Kevin -- Kevin Klop {uunet|rutgers|amiga}!cbmvax!kevin Commodore-Amiga, Inc. The number, 111-111-1111 has been changed. The new number is: 134-253-2452-243556-678893-3567875645434-4456789432576-385972 Disclaimer: _I_ don't know what I said, much less my employer.