Re: Strange C behaviour
On Tue, 4 Apr 2000 14:54:29 +0200, Michael Meskes <meskes@debian.org> said:
> When I started learning C (okay, that's quite some 15 years ago) I think I
> learned that an array is the same as a pointer. So I'd expect it to be
> pointer to the pointer.
If there are things that will slip even the eyes of the most
experienced C programmer, it is type promotion.
<book title="KnR, 1st ed.," citationfrom="Expert C Programming">
Every char in an expression is converted to an int .... Notice that
all the float's in an expression are converted to double....Since a
function argument is an expression, type conversions also take place
when arguments are passed to functions: in particular, char an dshort
become int, float becomes double.
</KnR>
<Book title="Expert C Programming[1]">
Original Type Usually promoted to
-------------------------------------------
char int
bit-field int
enum int
unsigned char int
short int
unsigned short int
float double
array of /anything/ pointer to /anything/
</Book>
So, you see: arrays _are_ pointers of a kind, just because an array
is converted to a (constant) pointer when used in an
expression. Therefore you can also use pointer[3] and array[3] in an
expression and the compiler does not even need special rules to tell
the difference, because it only needs the rule to access
*(pointer+3). (-8
> Michael
apologies to Peter van der Linden and
kind regards,
Andreas
[1] Title=Expert C Programming
subtitle=Deep C Secrets
Author=Peter van der Linden
Publisher=Prentice Hall/SunSoft Press
(I can only recommend every C Programmer to read this book. It is
amusing and very interesting to read)
--
Andreas Stefan Fuchs in Real Life aka
asf@acm.org, asfuchs@gmx.at, asf@ycom.at in NNTP and SMTP,
antifuchs in IRCNet and
Relf Herbstfresser, Male 1/2 Elf Priest in AD&D
Reply to: