0

I was given a solution to the problem of recreating the function strlen in C Programming and it was the following, I need help understanding what the code is actually doing.

void    *ft_memset(void *b, int c, size_t len)
{
    size_t i;

    i = 0;
    while (i < len)
    {
        ((unsigned char *)b)[i] = c;
        i++;
    }
    return (b);
}

My biggest doubts are the meaning of ((unsigned char *)b)[i] = c, specially because I don't understand the ( *)b, and the need for a return b in a void function.

Thank you very much for your help, sorry if I did something wrong its my first post in stack overflow.

7
  • 1
    That's not strlen :). And that "funny" thing from code is a pointer cast: from void * (a generic pointer - or memory address) to unsigned char * (which can be seen as a sequence of characters). Commented Dec 16, 2022 at 8:15
  • If this is an academic exercise, then perhaps you want to avoid looking at existing code, not even at code that is not strlen. Commented Dec 16, 2022 at 8:21
  • 4
    By the way the function you are looking at does not return void. Commented Dec 16, 2022 at 8:22
  • @n.m. It's not void (the star is before the name). Commented Dec 16, 2022 at 8:26
  • 2
    Looks like memset, not strlen and the name ft_memset suggests it as well. Commented Dec 16, 2022 at 8:27

2 Answers 2

1

Let's break it down, albeit my C skills are somewhat rusty. Edits are welcome :)

void    *ft_memset(void *b, int c, size_t len)
{
    size_t i;

    i = 0;
    while (i < len)
    {
        ((unsigned char *)b)[i] = c;
        i++;
    }
    return (b);
}

First, you have declared a function called ft_memset which returns void *.

void * means a pointer to any object. It's just a number, pointing to a memory address. But we do not know what's in it because it's not properly annotated.

Your function takes three arguments: b which is a pointer to anything (or void pointer if you will) c which is an int 32-bit signed integer number. len which is a size_t which is usually an alias to an unsigned integer. Read more about size_t over here


Your function iterates through the first len bytes and sets the c's value to those.

We're basically overwriting b's contents for the first len bytes.


Now, what does ((unsigned char*)b) mean. This statement is casting your void * to an unsigned char* (byte pointer), this is just telling the compiler that we're gonna deal with the contents pointed by b as if they were just a unsigned char.

Later we're just indexing on the i-th position and setting the value c in it.

Sign up to request clarification or add additional context in comments.

5 Comments

It's important to understand that memset deals with raw bytes, not array items.
<void * means a pointer to "anything"> better as <void * means a pointer to any object>, Function pointers might not fit in a void *.
Adding the edits as they come in <3
Does "it" in "it was just" refer to "contents pointed by b" (a plural - so use "they were just") or to "b" (singular)?
The first. I'll use the plural
1

Understanding code of function strlen

void *ft_memset(void *b, int c, size_t len) is like void *memset(void *s, int c, size_t n). They both assign values to the memory pointed to by b. These functions are quite different form strlen().

size_t strlen(const char *s) does not alter the memory pointed to by s. It iterates through that memory looking for a null character.

size_t strlen(const char *s) {
  const char *end = s;
  while (*end != '\0') {
    end++;
  }
  return (size_t)(end - s); 
}

// or

size_t strlen(const char *s) {
  size_t i = 0;
  while (s[i] != '\0') {
    i++;
  }
  return i; 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.