Changing the content of void *

D

der

Hello all,

I've a question about functions that takes as an argument a void *, and
change its content. e.g, meset(void *s, int c, size_t n);

Why does it work? The only solution I can think of is that these functions
will define a local pointer-to-pointer of a specific type(int in memset()
?), and manipulate the content of the void * with the pointer-to-pointer.
If so, how would they know the type of the pointer-to-pointer?

here is a code that explains my thought:

int
main(void)
{
char *c = malloc(1); /* just one byte */
void func(void *);
func(c);
printf("%c\n", *c);
return 0;
}
void
func(void *c)
{
char **p;
p = (char **) &c;
**p='v';
}

Thanks a whole lot for you great help.
 
P

pete

der said:
Hello all,

I've a question about functions that
takes as an argument a void *, and
change its content. e.g, meset(void *s, int c, size_t n);

void *meset(void *s, int c, size_t n)
{
unsigned char *p = s;

while (n--) {
*p++ = (unsigned char)c;
}
return s;
}

meset has the same return value and side effects as memset.
Standard library functions need not have definitions written in C.
Why does it work?
The only solution I can think of is that these functions
will define a local pointer-to-pointer of a specific
type(int in memset() ?)

A pointer to unsigned char, is what's called for, in memset.

N869
7.21.6.1 The memset function
Synopsis
[#1]
#include <string.h>
void *memset(void *s, int c, size_t n);
Description
[#2] The memset function copies the value of c (converted to
an unsigned char) into each of the first n characters of the
object pointed to by s.
Returns
[#3] The memset function returns the value of s.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top