I'm having trouble working with size_t*
and uint32_t*
I have a program that has something like
int main ()
{
size_t out_length;
.
.
.
func_1(param_1, ..., &out_length);
out_length > some_numberl;
}
func_1(param_1, ... , size_t *length)
{
.
.
.
*length = 0;
.
.
*length = to_some_unsigned_number;
}
As it is calling func_1();
in main
works fine, but now I have to call func_1();
inside another function that looks like this
func_2(param_1, ...,uint32_t* output_length)
{
}
so the program looks like this now
int main ()
{
size_t out_length;
.
.
.
func_2(param_1, ..., &out_length);
out_length > some_number;
}
func_2(param_1, ...,uint32_t* output_length)
{
func_1(param_1, ... ,output_length);
//stuff
}
func_1(param_1, ... , size_t *length)
{
.
.
.
*length = 0;
.
.
*length = to_some_unsigned_number;
}
so my question is how to appropriately obtain the value of output_length
. I currently read 0s.
I'll appreciate if someone could point me to the right direction.
You cannot store the size_t
value returned by f1
directly into the uint32_t
variable pointed to by the f2
argument, since the types are different, and the sizes can be different, too. Instead, use an intermediate local in f2
, which also gives a chance to test for out of range conditions. Same goes for the uint32_t
value returned by f2
to main
, just in the opposite direction.
#include <stdlib.h>
#include <stdint.h>
void func_1(size_t* length)
{
*length = 42;
}
void func_2(uint32_t* output_length)
{
size_t length;
func_1(&length);
if(length > UINT32_MAX) exit(1); // out of range
*output_length = (uint32_t)length;
}
int main()
{
size_t out_length;
uint32_t f2_length;
func_2(&f2_length);
out_length = f2_length;
}