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

  • Home
  • SEARCH
  • 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 495879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:37:59+00:00 2026-05-13T05:37:59+00:00

I am writing the memset function and my code is below, I am having

  • 0

I am writing the memset function and my code is below, I am having a problem

void* memsetFun(void* pointer, int c, int size) {

  if ( pointer != NULL && size > 0 ) {

    unsigned char* pChar =  pointer;


    int i = 0;

      for ( i = 0; i < size; ++i) {

      unsigned char temp = (unsigned char) c;

      *pChar++ = temp; // or pChar[i] = temp (they both don't work)

    }
  }  
    return pointer;


}

I also tried pChar[i] = the value we want and still not working. It gives me some trash numbers that do not make any sense.

And I am calling it:

memsetFun(address, num, size);
printf("value at %p is %d\n", address, *((int*) address));

Where I call the address (I just input the address)

For example, if you to print the chars ( c ) it prints like a weird char that looks like ( for the value 4 )

0 0
0 4
  • 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-05-13T05:38:00+00:00Added an answer on May 13, 2026 at 5:38 am

    Your code looks fine to me and several people here have commented that it works on their system.

    So the obvious thing to do is to debug it – that’s a skill that will come in handy quite a bit in future 🙂 You should learn it now.

    What does the following code output when you run it?

    void* memsetFun(void* pointer, int c, int size) {
        printf("A %x %d %d\n", pointer, c, size);
        if ( pointer != NULL && size > 0 ) {
            printf("B\n");
            unsigned char* pChar =  pointer;
            int i = 0;
            for ( i = 0; i < size; ++i) {
                printf("C %d (%d)", i, *pChar);
                unsigned char temp = (unsigned char) c;
                *pChar++ = temp; // or pChar[i] = temp (they both don't work)
                printf(" -> (%d)", i, *(pChar-1));
            }
        }  
        printf("D\n");
        return pointer;
    }
    

    From the output, it should be clear what paths the code is taking and what your parameters are (which will greatly assist the debugging process).

    Update:

    If you’re filling your memory block with anything other than zeros and using this:

    printf("value at %p is %d\n", address, *((int*) address));
    

    to print it out, you will get strange results.

    You’re basically asking for a number of those bytes to be interpreted as an integer. So, for example, if you filled it with 0x02 bytes and you have a 4-byte integer type, you will get the integer 0x02020202 (33686018), not 0x02 as you may expect. If you want to see what the first character value is, use:

    printf("value at %p is %d\n", address, *((char*) address));
    

    And based on your latest question update:

    For example, if you to print the chars ( c ) it prints like a weird char that looks like ( for the value 4 )
    0 0
    0 4

    If that’s a single character and you’re printing it as a character, there’s probably nothing wrong at all. Many output streams will give you that for a control character (CTRL-D in this case, ASCII code 4). If you instead filled it with ASCII code 0x30 (48), you would see the character ‘0’ or ASCII 0x41 (65) would give you ‘A’.

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

Sidebar

Related Questions

I'm trying to optimize the following code below to avoid having to copy and
I am writing essentially the equivalent of memset() using ASM. The code I have
Writing Classic ASP code in JScript has a lot going for it: more humane
Writing documentation in html requires some code examples. What to do with characters that
When writing code, I often place debug messages in the code. The debug messages
valgrind is reporting uninitialized memory errors from code like this: unsigned char buf[100]; struct
Hi following code sample seems to have some problem or either it has to
I am having a hard time in manipulating strings while writing module for linux.
Writing/reading code seems less stress then preparing a deploy scripts such as ./configure then
After writing a sample code for the question about converting between timezones , one

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.