home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!munnari.oz.au!metro!mama!radics!zoltan
- From: zoltan@research.canon.oz.au (Zoltan Kocsi)
- Subject: Re: extern: interesting case...
- In-Reply-To: bibhas@pico.engr.mun.ca's message of Wed, 6 Jan 1993 17:30:39 GMT
- Message-ID: <ZOLTAN.93Jan8133952@radics.research.canon.oz.au>
- Originator: zoltan@radics
- Sender: news@research.canon.oz.au
- Organization: Canon Information Systems Research, Australia
- References: <bibhas.726341439@femto.engr.mun.ca>
- Date: Fri, 8 Jan 1993 03:39:52 GMT
- Lines: 51
-
- In article <bibhas.726341439@femto.engr.mun.ca> bibhas@pico.engr.mun.ca (Bibhas Bhattacharya) writes:
-
- When I declare:
- extern char line[];
- for the variable "line" which was defined in another file as:
- char line[80];
- everything works fine. But when I do:
- extern char *line;
- the program dumps core. I can't figure out the difference, especially when
- extern declaration is not supposed to reserve any memory or anything. When
- I looked up K&R, the correct declaration would be: extern char line[];.
-
- Can anyone please clarify the case.
- Thanks.
- Bibhas
-
- Well, the K&R book tells you the truth. The declaration of an external object
- tells the compiler the type and name of the object and the fact that the
- actual memory allocation of the object is done in an other file.
- That's why, when you write extern char x[] it declares an array of chars
- named x. The actual size of the array is not required as
- the compiler need not to allocate memory for it. This declaration
- declares x as an array, that is, a memory location where _the_characters_in_
- the_array_ can be found. On the other hand, if you declare 'extern char *y' it
- tells the compiler that y is pointer ( a (probably) four-byte object ), the
- pointed object is character and the memory for the _four_bytes_ is allocated
- somewhere else. So when you write x[ 3 ], the compiler gets the address that
- x itself symbolyses, adds 3 to it then accesses the byte at this address.
- That's what you want. When you wrote y[ 3 ], the compiler got the address
- meant by y, then read four bytes from this location (dereferenced the pointer)
- handled this value as the start address of the array, added 3 to it and
- accessed the byte at this address. Because at memory location y there was not
- a pointer but some ASCII characters, the result address was rubbish, and you
- were lucky that it pointed out of your memory segment causing the coredump.
- Though the K&R book emphasyses the relationship between arrays and pointers,
- it never tells that they are the same. In fact, you can write y = &my_char;
- but you can never change the value of x ( being a symbolic constant ).
- Also, &x == &(x[0]) == x but &y != &(y[0]) == y.
- There is a case in C where the two declarations identical:
- If you have a function that has an input parameter of an array you can really
- declare it either x[] or *y. It is because C never passes
- arrays to functions, it _always_ passes the address of the array, that is
- a pointer to the array. In that sense the declaration x[] is misleading as
- the object you got from the caller is _a_pointer_ and not an array. (The
- compiler internally transforms your x[] declaration to *x if it declares
- an input parameter of a function.)
-
- Zoltan.
-
- --
- Zoltan Kocsi <zoltan@research.canon.oz.au>
-