From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII X-Google-Thread: 103376,bd45e29f9dafca87 X-Google-Attributes: gid103376,public From: gisle@kondor.ii.uib.no (Gisle S�lensminde) Subject: Re: bitwise comparators Date: 2000/01/19 Message-ID: #1/1 X-Deja-AN: 574712641 Content-Transfer-Encoding: 8bit References: <3880D375.7E363123@hotmail.com> <38829638.0@news.pacifier.com> <3882FC1C.2BA8C959@hotmail.com> <85vmn2$ki1$1@nnrp1.deja.com> <38836CF2.AB738B8B@hotmail.com> <3883A414.8559E641@earthlink.net> Organization: University of Bergen, Norway Content-Type: text/plain; charset=iso-8859-1 Mime-Version: 1.0 Newsgroups: comp.lang.ada Date: 2000-01-19T00:00:00+00:00 List-Id: In article , Keith Thompson wrote: >Jeff Carter writes: >[...] >> > With true arrays, do you mean out of bound checking, etc? This can be done >> > with _proper_ programming in C! >> >> C does not have arrays; it only has different notations for address >> arithmetic. > >That's a slight exaggeration. C does have array types and array >objects. For example this: > int a[10]; >declares a as an array of 10 ints, very much like Ada's > A: array(0 .. 9) of Integer; >It does not, contrary to popular misconception, declare a as a >pointer. > >What often causes confusion is that, in most expression contexts, a >reference to the name of an array object "decays" to a pointer to the >array's first element. > >C arrays are not first-class types, but they do exist. The semantics of C array is confusing for a lot of people, and they are frequently incorrectly used. C arrays are passed by reference to functions, but not if they are placed inside a struct. I have frequently seen code like this one: (Which not even gives warning on several compilers, like sunpro CC) char* dig2hex(char digest[20]){ int i; char hexstr[40]; /* insert calculation here */ return hexstr; /* returns a pointer to a local - may corrupt data later*/ } This code returns a pointer to a local variable, and since these data is placed on the stack, they will typically be overwritten later. Instead you can place the array inside a struct, like this: typedef struct hexstruct { char hexstr[40]; } hex; hex dig2hex(char digest[20]){ int i; hex tmphex; /* insert calculation here */ return tmphex; /* return the struct including array by copy - ok*/ } As long as the array is placed inside a struct you get by copy semantics, while an array on it's own behaves like a different notation for a pointer. A strange behavior here, is that the struct must be typedefed. Otherwise it won't compile. -- Gisle S�lensminde ( gisle@ii.uib.no ) ln -s /dev/null ~/.netscape/cookies