#Troubles passing through a struct as a function param

1 messages · Page 1 of 1 (latest)

lament notch
#

Hey all, i'm having troubles passing through a struct as a function parameter.
Some example code:

    pub var fileHandle: rsdk.std.fs.File = undefined;
};```
```const fileInfo: *reader.FileInfo = undefined;
reader.loadFile(&fileInfo);```
```pub fn loadFile(fileInfo: *FileInfo) i32 {
fileInfo.*.fileHandle = std.fs.cwd().openFile("Sfx/Music.ogg", .{}) catch {
    // TODO: Logging
    return 0;
};```

When I do this, I get this error: `error: no field named 'fileHandle' in struct 'Reader.FileInfo'`. Am I passing through the struct wrong?
regal hill
#

pub var name doesn't declare a field, it declares a global variable in the namespace represented by the struct

#

to declare a field of the struct, it's written file_handle: T,

#

so in this case, file_handle: rsdk.std.fs.File = undefined,

#

also, you don't have to write file_info.*.file_handle, you can omit one level of pointer indirection

#

so you can just do file_info.file_handle, even if the parameter is file_info: *FileInfo

lament notch
#

oh, didn't realise i was declaring fields wrong the whole time, haha. also didn't know that i didn't have to dereference it to access the fields. that fixed it, thank you!