Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8527803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:39:32+00:00 2026-06-11T08:39:32+00:00

I would have preferred to add a comment to the answer to this question

  • 0

I would have preferred to add a comment to the answer to this question
but didn’t have enough points. Consider the following code:

enum _config_error
{
    E_SUCCESS = 0,
    E_INVALID_INPUT = -1,
    E_FILE_NOT_FOUND = -2, /* consider some way of returning the OS error too */
    ...
};

/* type to provide in your API */
typedef _config_error error_t;

/* use this to provide a perror style method to help consumers out */
struct _errordesc {
    int  code;
    char *message;
} errordesc[] = {
    { E_SUCCESS, "No error" },
    { E_INVALID_INPUT, "Invalid input" },
    { E_FILE_NOT_FOUND, "File not found" },
    ...
};

How does one lookup the error description from errordesc? I can see two problems with the version I come up with:

/* add E_COUNT = 3 to enum _config_error */
const char *errorstring(error_t errnum)
{
    unsigned int i;

    for (i = 0; i < E_COUNT; ++i) {
        if (errordesc[i].code == errnum) {
            return errordesc[i].message;
        }

    }

    return "Can't reach this point";
}
  1. One does know the enum size and has to manually set E_COUNT to 3.
  2. One cannot reach the return after the for loop, what to do there?
  3. Is there a better solution?
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T08:39:34+00:00Added an answer on June 11, 2026 at 8:39 am
    1. You can calculate E_COUNT from sizeof(errordesc) / sizeof(struct _errordesc).
    2. If you reach the end of the loop, simply return "Unknown error" or something similar.
    3. Since your error codes seems to be consecutive (but negative) you could index directly into the array using -errnum.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought this question would have already existed on SO, but then I couldn't
I would have thought this one would be simple but I'm trying to change
I would have thought that this would be an easy thing to do, but
I am sure this would have been asked before but couldn't find it. Is
I would have expected the following C# program to only print EOF! once I
I suppose similar problem would have been discussed here, but I couldn't find it.
I have this code: award_dict = { "url": "http://facebook.com", "imageurl": "http://farm4.static.flickr.com/3431/3939267074_feb9eb19b1_o.png", "count": 1, }
I have this app, originally written for iOS 2.x. I recently needed to add
If you have code (objective-c) within a method that you would like to condense
I have the following situation: in a JCombobox, the preferred size is based on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.