hmelman@absolut.rtp.dg.com (Howard Melman) (07/27/90)
I'm having some problems with an external declaration of a character
array. I have some vague ideas why, but rather than guessing I'd rather
get some definitive answer from someone. Here goes:
I have the file declare.c:
char temp[20]="blah";
I have the file use.c:
#include <string.h>
extern char *temp;
main()
{
return(strcmp(temp,"blah");
}
This causes a core dump. When I change my extern declaration in use.c to be:
extern char temp[];
the program works fine. I was under the impression that the two
declarations were the same. Help...
Howard
steve@taumet.com (Stephen Clamage) (07/31/90)
hmelman@absolut.rtp.dg.com (Howard Melman) writes: >I have the file declare.c: >char temp[20]="blah"; >I have the file use.c: >extern char *temp; >This causes a core dump. As well it should. This is, I believe, in the Frequently Asked Questions list, or should be. The declarations "pointer-to" and "array-of" are NOT equivalent types. There are some expression contexts where either type may be used with the same semantics. "char *temp[20];" means "temp is the first address of an array of 20 chars". "temp[n]" means "n bytes past temp". "char *temp;" means "temp is a pointer variable which contains the address of a char". "temp[n]" means "n bytes past *temp". -- Steve Clamage, TauMetric Corp, steve@taumet.com
bryan@iconsys (Bryan Cardoza) (07/31/90)
In article <674@dg.dg.com> hmelman@absolut.rtp.dg.com (Howard Melman) writes: >I have the file declare.c: > >char temp[20]="blah"; > >I have the file use.c: ... >extern char *temp; ... >This causes a core dump. When I change my extern declaration in use.c to be: > >extern char temp[]; > >the program works fine. I was under the impression that the two >declarations were the same. Help... No so. When using *temp, the contents of temp are used, whereas when using temp[], the address of temp is used. In M68k assembly (using the System V/68 assembler syntax) we are talking about the difference between mov.l temp,%a0 for *temp and mov.l &temp,%a0 for temp[]. Sure, you can often use the two the same way in your C programs, but remember, we're talking about a storage declaration here, and pointers and arrays are not the same thing. -- Bryan Cardoza UUCP: uunet!iconsys!bryan Software Engineer Internet: bryan@iconsys.icon.com Icon International, Inc. (801) 225-6888 Orem, Utah FAX: (801) 226-0651
chris@mimsy.umd.edu (Chris Torek) (08/02/90)
In article <364@taumet.com> steve@taumet.com (Stephen Clamage) writes: >... The declarations "pointer-to" and "array-of" are NOT equivalent >types. There are some expression contexts where either type >may be used with the same semantics. (right) >"char *temp[20];" means "temp is the first address of an array of 20 chars". >"temp[n]" means "n bytes past temp". This must be a typo: `char temp[20]', not `char *temp[20]'. In this case temp *is* an array of 20 chars. It *becomes* the address of the first one, in those expression contexts, but it *is* an array. Think `object temp, array; value of object temp, pointer'. It is the `value' of an array that becomes a pointer: the array itself remains an array, always. -- In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163) Domain: chris@cs.umd.edu Path: uunet!mimsy!chris
hmelman@absolut.rtp.dg.com (Howard Melman) (08/02/90)
In article <61@iconsys>, bryan@iconsys (Bryan Cardoza) writes: >In article <674@dg.dg.com> hmelman@absolut.rtp.dg.com (Howard Melman) writes: >I have the file declare.c: > >char temp[20]="blah"; > >I have the file use.c: ... >extern char *temp; ... >This causes a core dump. When I change my extern declaration in use.c to be: > >extern char temp[]; > >the program works fine. I was under the impression that the two >declarations were the same. Help... I originally posted this question, I know understand it. To everyone who responded kindly I thank you. To everyone else who responded, I thank you too. To the newsgroup I appologize for posting something that is in the Frequently Asked Questions posting. The explanation in there is one of the best that I've read. People might want to look at it as several of the responses gave incorrect answers to my problem. To anyone who is thinking of posting to this newgroup with a question, be very careful of your wording or else you will get many responces to the wrong question, or just people annoyed at your ignorance. To everyone that did answer, I'm sorry that I can send a mail reply to you, but that is another problem I have to solve, thanks anyway. Howard
george@hls0.hls.oz (George Turczynski) (08/03/90)
In article <364@taumet.com>, steve@taumet.com (Stephen Clamage) writes: > > ... > > "char *temp[20];" means "temp is the first address of an array of 20 chars". > > ... What ??? Don't you mean "char *temp[20];" means "temp is the first address of an array of 20 (char *) (pointers)." Your description implies that 20 bytes of storage are used, but I think you'll find it is very different in reality ! Try this: /* Cut here */ #include<stdio.h> char *temp[20]; main() { fprintf(stderr,"Size of temp is %d.\n",sizeof(temp)); exit(0); } /* Cut here */ This should prove to you that what you said is WRONG. George P. J. Turczynski. | ACSnet: george@highland.oz | Phone: 61 48 683490 Computer Systems Engineer. | Fax: 61 48 683474 |---------------------- Highland Logic Pty. Ltd. | I can't speak for the Suite 1, 348-354 Argyle Street | company, I can barely Moss Vale. NSW. 2577 Australia | speak for myself...