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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:42:47+00:00 2026-06-14T15:42:47+00:00

gcc 4.7.2 c89 bstrlib.h from Better String Library I am maintaining someone’s code that

  • 0
  • gcc 4.7.2
  • c89
  • bstrlib.h from Better String Library

I am maintaining someone’s code that looks like this:

FILE *db_fp = NULL;
bstring data = NULL;

db_fp = db_open(DB_FILE, "r");
LOG_CHECK(db_fp == NULL, "Failed to open database: %s", DB_FILE);

data = bread((bNread)fread, db_fp);
LOG_CHECK(data == NULL, "Failed to read from db file: %s", DB_FILE);

db_close(db_fp);

return data;

I am having a little trouble understanding the following line:

data = bread((bNread)fread, db_fp);

What I can guess, is that it is fetching a bstream from the following file pointer and returning a bstring. However, I am wondering about bread and fread.

bread contains 2 arguments, A function pointer (bNread) and the file pointer. But I am not sure I understand how it works.

  • 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-14T15:42:48+00:00Added an answer on June 14, 2026 at 3:42 pm

    The declaration for bread() in the Better String Library documentation is:

    typedef size_t (*bNread)(void *buff, size_t elsize, size_t nelem, void *parm);
    extern bstring bread(bNread readPtr, void *parm);
    

    Therefore, bread() is a function that takes a pointer to a function as an argument. The bNread type is used to specify the type of function. The fread() function is close enough to pass muster when cast — the match isn’t exact because it expects a FILE * for its fourth argument and a true function of the type pointed at by a bNread expects a void *. The second argument to bread is the value to be passed as the fourth argument to the function pointed to by the bNread.

    So, in the code you’re maintaining:

    data = bread((bNread)fread, db_fp);
    

    The Better String Library function is called with fread() as the I/O function, cast to the correct type to quell an otherwise justified compiler warning about a mismatch in types, plus the file stream that should be used for reading.


    The implementation of the bread() function uses the function pointer and the stream pointer whenever it needs to do I/O in order to read a string. That is, as KerrekB also explained, the code
    might write something like:

    char buffer[256];
    
    size_t nbytes = (*readPtr)(buffer, sizeof(char), sizeof(buffer), parm);
    

    or (equivalently):

    size_t nbytes = readPtr(buffer, sizeof(char), sizeof(buffer), parm);
    

    (If you learned C long enough ago, the first was the only way to invoke functions through pointers to functions; since the C89 standard was produced, the second has been available and is probably used more widely these days.) There are a number of tricks that the implementation must worry about, but the basic function call would be somewhat similar. The code could use I/O functions other than fread(), though the interface to the function must be similar.

    Pointers to functions are powerful, but are arcane until you’ve used them.

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

Sidebar

Related Questions

gcc 4.5.1 c89 I am maintaining someone's code. And they have got things really
gcc 4.4.2 c89 I have this code snippet that I have to repeat in
gcc (GCC) 4.6.0 c89 I have a signal handler declared like this: /* Setup
gcc c89 I am came across this code. typedef __int32 int32_t; typedef unsigned __int32
gcc 4.6.1 c89 I have a string that I need parse. The string is
gcc c89 I am just looking to link against the below shared library. I
gcc 4.4.2 c89 I have a function that has to run (config_relays). It make
gcc (GCC) 4.6.3 c89 Hello, I am just wondering if this is the best
gcc 4.6.2 c89 I have the following 2D array that I want to pass
gcc 4.4.1 c89 I have the following code snippet: #include <stdlib.h> #include <stdio.h> char

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.