From owner-freebsd-questions Mon May 21 15:26:41 2001 Delivered-To: freebsd-questions@freebsd.org Received: from ewey.excite.com (ewey-rwcmta.excite.com [198.3.99.191]) by hub.freebsd.org (Postfix) with ESMTP id 80E9337B424 for ; Mon, 21 May 2001 15:26:36 -0700 (PDT) (envelope-from john_wilson100@excite.com) Received: from zero.excite.com ([199.172.152.241]) by ewey.excite.com (InterMail vM.4.01.02.39 201-229-119-122) with ESMTP id <20010521222631.LYB11367.ewey.excite.com@zero.excite.com> for ; Mon, 21 May 2001 15:26:31 -0700 Message-ID: <11895256.990483991242.JavaMail.imail@zero.excite.com> Date: Mon, 21 May 2001 15:26:30 -0700 (PDT) From: John Wilson To: freebsd-questions@freebsd.org Subject: Question for C preprocessor gurus Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Excite Inbox X-Sender-Ip: 62.90.91.30 Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.ORG In this program I'm writing, I need to define a simple list of errors, e.g. #define ERR_OK 0 #define ERR_BAD 1 #define ERR_AWFUL 2 and a function to return error descriptions: char *f(int error_no) { switch (error_no) { case ERR_OK: return "OK"; case ERR_BAD: return "Bad"; /* .... */ } } There is nothing wrong with this code, except one thing - to add a new error, I need to do it in two different places. I was therefore wondering if it was possible to avoid adding the same strings twice through (ab)use of the preprocessor. Of course, I could do something like this: static char *Errors[] = { "ERR_OK", "ERR_BAD", "ERR_TERRIBLE" } and then define char* ErrorDescription(int error_code) { if ((error_code >= 0) && (error_code < sizeof(Errors)/sizeof(Errors[0]))) return Errors[error_code]; else return "UNDEFINED ERROR"; } but then I wouldn't be able to refer to the errors by name. Ideally, I would like to say DEFINE_ERROR(ERR_OK, 0) DEFINE_ERROR(ERR_BAD,1) and have the preprocessor generate something like this: static char *Errors[] = { #define ERR_OK 0 "ERR_OK", #define ERR_BAD 1 "ERR_BAD" } Any ideas? Thanks John Wilson _______________________________________________________ Send a cool gift with your E-Card http://www.bluemountain.com/giftcenter/ To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-questions" in the body of the message