#strings of unknow size

1 messages · Page 1 of 1 (latest)

wintry hornet
#

i am tring to get a string as an input to a function that i dont know the size of:

pub fn findTable(signature: *const []u8) ?*SDTHeader {
    const localRsdp = tables.rsdp orelse {
        virtio.printf("can't find table rsdp not found\n", .{});
        return null;
    };
    const rsdt = localRsdp.rsdt_address;
    const enteries = (rsdt.h.length - @sizeOf(SDTHeader)) / @sizeOf(u32);
    const enteriesArray: [*]*SDTHeader = @ptrCast(&rsdt.entries);
    for (0..enteries) |i| {
        const header: *SDTHeader = enteriesArray[i];
        if (std.mem.eql(u8, &header.signature, signature)) {
            return header;
        }
    }
    virtio.printf("table not found with signiture {}\n", .{signature});
    return null;
}

but it gives the error:

src/kernel/arch/x86/acpi.zig:170:31: error: expected type '*const []u8', found '*const [4:0]u8'
    return @ptrCast(findTable(FADT_SIGNATURE));

also if you find any problems in my code or things that i can do better tell me

left willow
#

*const []u8 is a pointer to a constant slice of u8s - so a double pointer. this is probably not what you want...

wintry hornet
#

then how should i approch this?

left willow
#

strings in Zig are usually represented by []const u8s - slices to constant u8s

wintry hornet
#

ohhhh

#

i tried like []u8

#

so a *cont []u8 is a pointer to a [] const u8?

left willow
#

*const []u8 is a pointer to a constant []u8

#

[]u8 is in turn a pointer-length pair

wintry hornet
#

yeah but that tells me cant convert

#

only when i say [] const u8

left willow
#

that's because they can't

wintry hornet
#

then why does it represent it as *cont []u8 and not *cont [] const u8

#

does the first const make the second part also a const?

#

or because it is a const slice

#

ohhhhhh

left willow
#

here's a doodle for the memory layout of the two types:

wintry hornet
#

yeahhhhhhh me understand

left willow
#

*const []const u8 is a pointer to a constant slice to constant u8s.
*const []u8 is a pointer to a constant slice to u8s (not const!).

wintry hornet
#

then why cant i convert *const []u8 to a []u8 but i can to []const u8

#

that is the confusing part

#

or do i need to do a .*

left willow
#

*const []u8 can't be converted to a []u8 - it needs a dereference
*const []u8 can't convert to []const u8 either

wintry hornet
#

but it does

#

it converts to a [] const u8

#

doesnt complain

#

conertion

left willow
#

you're probably confusing it with something else - can you share the code which you say does this conversion?

wintry hornet
#

String literals are constant single-item Pointers to null-terminated byte arrays. The type of string literals encodes both the length, and the fact that they are null-terminated, and thus they can be coerced to both Slices and Null-Terminated Pointers. Dereferencing string literals converts them to Arrays.

#
pub fn findTable(signature: []const u8) ?*SDTHeader {
    const localRsdp = tables.rsdp orelse {
        virtio.printf("can't find table rsdp not found\n", .{});
        return null;
    };
    const rsdt = localRsdp.rsdt_address;
    const enteries = (rsdt.h.length - @sizeOf(SDTHeader)) / @sizeOf(u32);
    const enteriesArray: [*]*SDTHeader = @ptrCast(&rsdt.entries);
    for (0..enteries) |i| {
        const header: *SDTHeader = enteriesArray[i];
        if (std.mem.eql(u8, &header.signature, signature)) {
            return header;
        }
    }
    virtio.printf("table not found with signiture {s}\n", .{signature});
    return null;
}
left willow
wintry hornet
#

and it works i give it a *const [4:0]u8

left willow
wintry hornet
#

this is confusing

left willow
wintry hornet
#

i understand pointers just arrays and slices confuse me allitle, why can i convert *const [4:0]u8 to a [] const u8?

#

is that not an array to slice?

#

oh

left willow
wintry hornet
#

makes sense nowww

#

because when u have a const array you cant change

#

ok

left willow
#

go watch the video, Loris does a good job explaining all of this