sizeof() with struct pointer

Hello folks,

i´m just curious is it possible to give a function a struct pointer and it can determine its length ?

typedef struct {
  byte r;
  byte g;
  byte b;
} RGB;
void write_struct(uint8_t i2c_address, uint8_t* struct_ptr, uint8_t length){
  Serial.println(sizeof(&struct_ptr));
}

Since struct is not a valid data type i used uint8_t* and length, any better way ?

Are you after the size of the struct, or the size of the pointer? It's hard to tell, because this:

uint8_t* struct_ptr

makes no sense. That is a pointer to an 8-bit unsigned int.

In any case, the argument to sizeof is always a type. So, if you have an int, you would use:

int intsize = sizeof(int);

If you have a struct myStruct, you would use:

int structsize = sizeof(struct myStruct);

If your struct has been declared using typedef, and is of type t_myStruct you would use:

int structsize = sizeof(t_myStruct);

Regards,
Ray L.

Since struct is not a valid data type

Because struct is a qualifier, not a type.

struct color
{
   byte r;
   byte g;
   byte b;
};

struct color RGB;

RGB.r = 14;

int size = sizeof(struct color);

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.

write_struct(0x11, (byte*)&example, sizeof(example));

void write_struct(uint8_t i2c_address, uint8_t* struct_ptr, uint8_t length){
  Serial.println(sizeof(&struct_ptr));
  Wire.beginTransmission(i2c_address);
  Wire.write(start);
  Wire.write(length);
  for(int i = 0; i<length; i++){    
    Wire.write(*(struct_ptr+i));
  }

yeah i just wanted to figure out if it if possible to determine the length in the function rather than giving it to the function.

Guess thats a no-go, thanks for the response !

Guess thats a no-go

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.

You are currently doing neither of these.

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.

--- bill

Few people seem to know this but 'sizeof' is an operator, not a function.

unsigned long time;
int time_size = sizeof time;

FYI: Similarly 'return' is an operator, not a function.

Thanks for the hints !

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.

hmm why can u call it as a function then ?

Greetings

You can't, you can just use extra parentheses, that are often not required.