Can I return a struct from a function? #1668
-
I am trying read some data from the file in a loop in a structured way to use for markup later. struct SubsystemTableEntry {
u16 numProperties;
u32 ptr;
};
fn readSubsystemTable(u32 i) {
// some argument asserts here
// systemTableOffset is const
SubsystemTableEntry systemTableEntry @ systemTableOffset + i * sizeof(SubsystemTableEntry);
// some asserts here
return systemTableEntry;
};
for (u32 i = 0, i < header.numSubsystems, i = i + 1) {
u32 numProperties = readSubsystemTable(i).numProperties;
for (u32 j = 0, j < numProperties, j = j + 1) {
// mark up file regions here
}
} But I get an error:
Is it possible to return a struct from a function? Am I misusing operator |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Yes to the first two questions. You can return structs, but you cannot place patterns inside a function. Patterns can only be placed in the global scope. Also loop statements are considered as function statements so you can't place patterns inside for loops.Instead what you probably want to do is create an array of patterns that reads the data as it grows. |
Beta Was this translation helpful? Give feedback.
Yes to the first two questions. You can return structs, but you cannot place patterns inside a function. Patterns can only be placed in the global scope. Also loop statements are considered as function statements so you can't place patterns inside for loops.Instead what you probably want to do is create an array of patterns that reads the data as it grows.