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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:54:02+00:00 2026-06-16T04:54:02+00:00

I wanted to write a function that takes two arguments: a void pointer type

  • 0

I wanted to write a function that takes two arguments: a void pointer type to an arbitrary memory-block and a block bytesize. Knowing the struct type of data writen in the block, the function should print the values contained.

However, at first, the code I suggested didn’t work:

#define RECORD struct record
struct record {
      char nam[32];
      double val;
};

void xprint (void *p, long j)
{
    j /= sizeof(RECORD);
    RECORD r;

    while(j--){
        r = *((RECORD *)p++);
        printf("\n..%s.., ..%lf..\n",r.nam, r.val);
    }
    return;
}

So, I came up with some alternations, mainly in the incrementing part of the code:

void print (void *p, long j)
{
    j /= sizeof(RECORD);
    RECORD r = *((RECORD *)p);

    while(j--){
        printf("\n%s,\t%8.2lf\n",r.nam, r.val);
        r = *(++(RECORD *)p);
    }
    return;
}

Now it did the job, but still the code looks less compact.

After some inspection, I found the problem lies in r = *((RECORD *)p++); line. It seems that when it comes to a postfix incrementation, p is no longer typecasted, and hence p is incremented by one byte only.

Could the xprint function be rewritten so that I would still use the postfix operator, but applied to a typecasted pointer?

  • 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-16T04:54:03+00:00Added an answer on June 16, 2026 at 4:54 am

    Convert the void * to a RECORD * straight away and then use that pointer for the rest of the function.

    void print (const void *p, size_t size)
    {
        const RECORD *r = p;
        size_t count = size / sizeof(*r);
    
        while (count--) {
            printf("\n%s,\t%8.2lf\n", r->nam, r->val);
            ++r;
        }
    }
    

    I also made some stylistic changes here, such as better variable names and adding const.


    On a side note, as Clement Rey says it’d be better to use a typedef than a define.

    typedef struct record record_t;
    

    You can even combine the typedef with the struct definition:

    typedef struct {
        char nam[32];
        double val;
    } record_t;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to write a function that takes a block of strings, does
I wanted to write a function that'll be cross platform (win32 & linux), and
I wanted to write a function that would take an object and convert it
I'm trying to write a php function that takes the $name and $time and
I wanted to write a function that reverses all the sub-lists in a list
I'm playing around with beginner Haskell, and I wanted to write an average function.
I have to write my own hash function. If I wanted to just make
I wanted to write a method with an argument that defaults to a member
I wanted to write a Custom Control that would show a family tree... so
I'm a beginner in C++ Programming language. I wanted to write a program that

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.