zig build-exe --main-pkg-path .. --single-threaded -I/usr/include -L/usr/local/lib64 -L/usr/lib64 -lsqlite3 -lc pkg-config --libs gtk+-3.0 composite_register.zig
Semantic Analysis [783/1023] /home/dca/Software/newcash_zig/common/common.zig:308:47: error: expected pointer, found '[]const u8'
var account_names = mem.separate(@ptrCast([]const u8, account_path), ":");
^
I find "expected pointer, found '[]const u8'" puzzling, since the documentation says "[]T - pointer to runtime-known number of items. " in the Pointers section. Either I'm missing something or this is a bug.
Either I'm missing something or this is a bug.
@ptrCast is used to cast between pointer types and slices are _not_ pointers, they're just a pointer to some memory plus a length field that may change at runtime. Use std.mem.spanZ to convert your null-terminated char arrays into a slice with proper length.
I think the problem, at least for me, is that the Pointers section of the documentation is not clear. It does say at the outset that there are two kinds of pointers and then describes them. No issue there. It then goes on to say "These types are closely related to Arrays and Slices:", which is where the trouble starts for me. The ensuing description describes the array and slices syntaxes as "pointer to ...". So it is very unclear whether that notation is considered a pointer or not, especially since the word "pointer" is used in their descriptions.
Even in your comment above, you say slices are "not pointers" and then go on to describe them as "just a pointer ....". Either they are pointers or they aren't. If they aren't, the word pointer should not be used, especially in the official documentation.
But thanks for the tip on how to fix my code.
Either they are pointers or they aren't. If they aren't, the word pointer should not be used, especially in the official documentation.
Slices are just a pointer + a number indicating the length of the slice.
These types are closely related to Arrays and Slices
That's why they are related. Nothing more, nothing less.
You are continuing to overload the word "pointer", which is the primary issue of this discussion. I'm not going to continue to try to make you see that. I've stated my case that the documentation is confusing on this issue and should be fixed and will leave it at that.
I thought I saw some changes to make the @ptrCast unnecessary by automatically inferring the array type was being made recently?
It may be easier to think of slices as _fat_ pointers -- a struct that contains a data pointer and a length field:
slice = struct {
data: [*]type,
length: u64,
};
Personally, I've always disagreed with the notion that slices are "pointers". They are not. They are simply a view into a region of memory.
The fact that they "point" to a region of memory is presumably why they are described as a type of pointer - but they are not.
They are a struct--which yes, contains a pointer--but its purpose is simply so that you can pass around a region of memory.
@Tetralux Yes, I understand all that. Again, the issue is not my accepting and understanding that slices are not treated as pointers by zig, but rather my contention that the documentation is confusing on this issue and should be fixed. I've posted this because I'm trying to help the project, in a small way. Confusing newcomers with bad documentation on a key issue is not a good way to encourage acceptance of the language. I am a very experienced writer of software and manager of software projects (and the documentation thereof). I say this not to blow my own horn but to emphasize that if the documentation confused me, what about the young 'uns?
By the way, I think you explained it quite well in your post. Something along the lines of what you wrote could be the basis for a fix to the documentation. I'd also suggest moving the discussion of arrays and slices out of the "Pointers" section. Their inclusion there sets the stage for the confusion. If they aren't pointers, why are they being discussed in a section entitled "Pointers"?
Yes, I understand all that. Again, the issue is not my accepting and understanding that slices are not treated as pointers by zig, but rather my contention that the documentation is confusing on this issue and should be fixed
No, no. I completely agree. If it sounded otherwise, apologies - not my intention.
I'd also suggest moving the discussion of arrays and slices out of the "Pointers" section. Their inclusion there sets the stage for the confusion. If they aren't pointers, why are they being discussed in a section entitled "Pointers"?
I'm inclined to agree with this too.
Most helpful comment
Personally, I've always disagreed with the notion that slices are "pointers". They are not. They are simply a view into a region of memory.
The fact that they "point" to a region of memory is presumably why they are described as a type of pointer - but they are not.
They are a struct--which yes, contains a pointer--but its purpose is simply so that you can pass around a region of memory.