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 8156245
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:53:25+00:00 2026-06-06T16:53:25+00:00

I have a range of C functions: const char* find_string(uint8_t id); int32_t find_int32(uint8_t id);

  • 0

I have a range of C functions:

const char* find_string(uint8_t id);
int32_t find_int32(uint8_t id);
uint32_t find_uint32(uint8_t id);
uint64_t find_uint64(uint8_t id);

Which return the found item (in a linked list). For string I can return a null string. But for the integer values I want to indicate whether the integral value was found or not. What is the C idiom way to do this?

Like this: ?

uint32_t find_uint32(uint8_t id, int* success);
  • 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-06T16:53:28+00:00Added an answer on June 6, 2026 at 4:53 pm

    There’s no one way to accomplish this, but the typical solution is this:

    int find_uint32(uint8_t id, uint32_t *result)
    

    Letting the function return a status code rather than the result. The actual result is “returned” through a pointer. While opinions differ here, it might be a good idea to have the function return zero on failure, so that you can do things like:

    if (!find_uint32(42, &out)) {
        /* error */
    }
    

    One reason to have it the other way around, i.e., return the status code through a pointer parameter and the result as the function’s return value, is that you could allow passing NULL to the status code if errors are to be ignored. That way, if you for some reason need not worry about errors, your code will be more readable; consider:

    uint32_t val = find_uint32(42, NULL /* don't care */);
    

    In some cases you can reserve a “magic value” as an error indicator. As you mentioned, the NULL pointer works for strings. If you are sure that some integer value is never a valid result, you could let it indicate error. For instance, if you have a function that returns unsigned values that will never exceed 0x7FFFFFFF, you could let the 31st bit indicate error, leaving the remaining 31 bits to carry data or an error code depending on the value of the top bit.

    I would generally discourage this practice, the rationale being that magic error numbers will make the code harder to read and maintain than explicitly returning an error flag.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So currently in my programming I now have a fairly large range of functions
Many languages have functions which only process plaintext, not binary. Does this mean that
Good afternoon, I have a C++ class Range which implements a operator < for
I have a group of functions that use Sub-Range types for their input parameter.
I have a custom c++ module for python that exposes functions, some of which
I have a range of objects which needs to be ordered with a rule.
I have the following JQuery function about a range slider . It's just working
I have a javascript function that checks for a date range. Is there any
I have range of numbers that range from 1 - 0.00000X . Most are
Suppose I have range from E2 to E16. How do I read values from

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.