Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dimension declarations in 2D arrays reversed? #104

Open
ibara opened this issue Dec 3, 2020 · 4 comments
Open

Dimension declarations in 2D arrays reversed? #104

ibara opened this issue Dec 3, 2020 · 4 comments

Comments

@ibara
Copy link
Contributor

ibara commented Dec 3, 2020

Hello.

Here are two programs that ought to be equivalent, one in C and the other in Cowgol:
https://github.com/ibara/cowgol-utilities/blob/main/2darray.c
https://github.com/ibara/cowgol-utilities/blob/main/2darray.cow

However, they are not equivalent. The output from the C version is what I would expect but that is not the output from the Cowgol version. To get the expected output from the Cowgol version, swap the dimension index declaration on line 6. That is, put XMAX before YMAX in the Cowgol version, even though all other accesses are in the form [y][x].

This is a bit unexpected, so bringing it up in case it is not intentional.

Discovered when writing a Game of Life implementation in Cowgol.

@davidgiven
Copy link
Owner

Oh, interesting. That's not actually supposed to work! I've just had else on another forum complain about only having single-dimension arrays, too...

I will look into this; it's clearly almost working so it would be good to regularise. Thanks for the test case.

@dumblob
Copy link

dumblob commented Oct 18, 2023

Did not check the source but dare to ask anyway - is this already supported?

@shattered
Copy link
Contributor

sizeof for 2d arrays also returns unexpected values (bytesof is ok):

var mshorts: uint16[5][2] := { {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10} };

sizeof returns 2

@davidgiven
Copy link
Owner

Given the structure of the initialiser, I would expect sizeof in that case to return 2 --- there are two elements in the outermost array.

What's happening here is that clearly [] is associative in the wrong direction. Which is a shame as it's much easier to parse this way round.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants