Reputation: 521
So here is my problem.
I have a struct that has some properties:
struct foo {
const uint8_t propertyA;
int propertyB;
const int propertyC;
typeX propertyD;
typeY propertyE;
};
I then create an array of this struct since I have to represent multiple instances of the object foo:
const int foosQuantity = 8;
struct foo foos[foosQuantity] =
{{ .propertyA=0b11001100, .propertyB=0, .propertyC=140, ...},
{ .propertyA=0b11001000, .propertyB=0, .propertyC=150 ...},
//*** foosQuantity-3 MORE TIMES ***//
{ .propertyA=0b11001000, .propertyB=0, .propertyC=150 ...}}
Until now everything seems to work. However, I have not figured how to send an array of one type of property to a function. For example, I Have this function written in an extern library that takes an array of propertiesA:
void myFunc(const uint8_t *propertyAs, sizeArray){
//*** DO STUFF ***//
}
And I would like to send the properties of my struct directly without having to use a for loop that iterates through the struct array and copies the elements.
int main(){
//*** STUFF ***//
myFunc(foos.propertyA, foosQuantity);
//*** MORE STUFF ***//
return 0;
}
Is this possible?
Upvotes: 3
Views: 102
Reputation: 1180
This is not possible.
You can either
Option 1 is the most memory efficient since it requires the least memory but it may require you to rewrite your a lot of your code.
Option 2 requires the least amount of rework of your code but requires more memory and time.
Upvotes: 1
Reputation: 21542
If the entire struct array already exists in your program's memory, you aren't going to get more efficient than just passing a pointer to the start of the array, the array's length (in elements), and iterating on the propertyA
members of the array:
void bar(struct foo *p, size_t len)
{
size_t i;
for(i = 0; i < len; i++)
{
dosomething(p[i].propertyA);
}
}
If you are restricted by assigned specifications to only be allowed to pass an array of propertyA
(i.e. uint8_t
) to your function, you do have to copy them out of the array of struct foo
; there's no way around that.
Upvotes: 5