Wrong. You CAN determine the size of the struct in the function. But, you need to:
A) Define the struct correctly. Lose the typedef crap.
B) Pass a pointer to the struct instance to the function correctly.
typedefs are ok when declared properly.
And you can get the size using a typedef.
The key is that in order to get the size in the function the function must
know the type of the variable at compile time.
sizeof() is very simplistic and is calculated at compile time not a run time.
This means that you must properly declare your parameters and cannot
play any sort of overloading casting games.
Once you declare your structures, typedefs, variables, and the function properly,
you can then get the size
of the pointer itself (which is probably not what you are wanting)
or the size of what the pointer points to.
Example:
foo(type *ptr)
{
ptrsize = sizeof(ptr); // size of the pointer itself
ptrdatasize = sizeof(*ptr); // size of what the pointer points to
Just keep in mind that the function has no idea if the pointer is pointing to an array of objects
or how big that array might be so that value you get is size of a single instance of the data type declared.
But in the bigger picture perhaps you are using an array of RGB values in memory?
and simply want to advance through the array?
If that is the case, you can (should) let the compiler do pointer math for you.
Your code will not need to know the size of the structure.
A simple ptr++ will increment to the next instance in the memory array for you since
the compiler knows the size of the structure.
i.e. if ptr is declared as a pointer to an RGB structure type of 3 bytes,
then ptr++ will increment by 3.
Your use of typedef on an unnamed struct is what is causing your problems. You could get the size of an RGB since that IS a type.
Yeah that is right but i can determine the size (correctly) by using typedefs, the problem appears i do not know how to properly define a struct(or typedef in this case) pointer as parameter of a function.
Thats why i use
(byte*)&
, i just wondered how the code could look like.
I tried this solution
foo(type *ptr)
but i cant get it to compile, can you expand you example a bit ?
Few people seem to know this but 'sizeof' is an operator, not a function.