How to get a pointer on the return value of a function?
The function
I'm using the function uint32_t htonl(uint32_t hostlong) to convert a
uint32_t to network byte order.
What I want to do
I need to do calculations with the variable after converting it to network
byte order:
//Usually I do calculate with much more variables and copy them into a much
// larger buff - to keep it understandable and simple I broke it down
// to one calculation
uint32_t var = 1;
void* buff;
buff = malloc(sizeof(uint32_t));
while(var < 5) {
var = htonl(var);
memcpy(buff, &var, sizeof(uint32_t));
doSomethingWithBuff(buff);
var++; // FAIL
}
What I could do but ...
Actually I found a solutions for this problem already:
uint32_t var = 1, nbo;
void* buff;
buff = malloc(sizeof(uint32_t));
while(var < 5) {
nbo = htonl(var);
memcpy(buff, &nbo, sizeof(uint32_t));
doSomethingWithBuff(buff);
var++;
}
The problem is that I waste memory with this solution because nbo is just
used as a buffer.
What I would prefer to do
It would be perfect if I could use the htonl() function within the
memcpy() function. memcpy() needs the 2nd value to be a void*. My Question
is: How can I get the address of the return value of htonl()?
uint32_t var = 1;
void* buff;
buff = malloc(sizeof(uint32_t));
while(var < 5) {
memcpy(buff, (GET ADDRESS)htonl(var), sizeof(uint32_t));
doSomethingWithBuff(buff);
var++;
}
And if it is not possible because there "is no address of this variable":
How does a function work that is returning a variable rather than a
pointer to a variable?
No comments:
Post a Comment