Path: utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!iuvax!bsu-cs!dhesi From: dhesi@bsu-cs.UUCP (Rahul Dhesi) Newsgroups: comp.lang.c Subject: Re: hardcoded constants Message-ID: <5226@bsu-cs.UUCP> Date: 17 Dec 88 15:24:59 GMT References: <1988Dec8.173158.11839@utzoo.uucp> <846@starfish.Convergent.COM> <33604@think.UUCP> <2478@ficc.uu.net> Reply-To: dhesi@bsu-cs.UUCP (Rahul Dhesi) Organization: CS Dept, Ball St U, Muncie, Indiana Lines: 36 In article <2478@ficc.uu.net> peter@ficc.uu.net (Peter da Silva) writes: >How about sizeof "/"? Or does that return sizeof(char *)? I considered this and rejected it because it is misleading. We need 2 because one is for a '/' in the middle and one is for a trailing null, not because we need two for just '/'. That "/" is a string and needs two is not really the point, even though it gives the right answer. The format I prefer most is actually: strlen(a) + 1 + strlen(b) + 1 because it arranges the components of the expression in the right order. (It also avoids the magic number 2. However, 1 is a magic number here even though it isn't usually considered to be one.) It would be nice to be able to say strlen(a) + sizeof('/') + strlen(b) + 1 but unfortunately sizeof('/') is the same as sizeof(int). One could also do #define ONE_CHAR 1 #define TWO_CHARS 2 and then say things like: strlen(a) + ONE_CHR + strlen(b) + ONE_CHR strlen(a) + strlen(b) + TWO_CHARS This sounds like the type of error undergraduates make when they are first asked to use symbolic constants, but upon thinking I realize that it could actually clarify the code quite a bit. -- Rahul Dhesi UUCP: !{iuvax,pur-ee}!bsu-cs!dhesi