Closed thorpep138 closed 4 years ago
Indeed arrays automatically "slice" into sub-arrays. It's quite fundamental to the language and a very common pattern. It also works for by-reference arguments. The wiki is open so feel free to add an explainer in whatever form you think would be most helpful.
Though, maybe we should dump the wiki entry into markdown and check it in with the source tree.
Thanks for the note @dvander ! I just tried to update the docs at https://wiki.alliedmods.net/index.php?title=Introduction_to_SourcePawn_1.7&action=edit but it says I don't have permission to edit because I'm not in the users group. Can you add me? Also what is the source tree?
Oh derp with "source tree" that's just referring to version control. I agree that would be much better to version it.
Interestingly the upstream Pawn Language Guide doesn’t really explicitly document this, I could only see the following note (and no usage of the pattern in any of the examples) which isn’t really clear if you’re new to the language:
If the function argument is an array, the caller can pass to the function:
- an array with the same dimensions, whose starting address is passed;
- an (indexed) array element, in which case the address of the element is passed.
Thanks for the note @dvander ! I just tried to update the docs at https://wiki.alliedmods.net/index.php?title=Introduction_to_SourcePawn_1.7&action=edit but it says I don't have permission to edit because I'm not in the users group. Can you add me?
Make sure you’re logged in on the wiki using your forum account credentials, there should be no other requirements.
It should probably be part of the array documentation (possibly with a reminder in the string section).
Thanks for the details @asherkin . I will include those in the documentation update. However, I am still not able to login to wiki.alliedmods.net. I signed up for an account on mediawiki.org and am logged in there but not able to create an account or login at the edit page. Can someone help me?
You have to register at the forums and login using those credentials in the wiki. They are linked.
Thanks @peace-maker ! I updated https://wiki.alliedmods.net/Introduction_to_SourcePawn_1.7#Array_Parameters with details on the array slicing behavior. This issue can be closed!
Please update the SourceMod documentation to mention that dereferenced string array indices will be treated implicitly as pointers when used in string functions like strcopy.
For example, strcopy(newString, sizeof(newString), oldString[2]);
will copy the contents of oldString into newString starting from index 2, though the strcopy function makes no mention that this behavior is available at https://sm.alliedmods.net/new-api/string/strcopy nor is any reference made in the SourcePawn string documentation at https://wiki.alliedmods.net/Introduction_to_SourcePawn#Strings nor anywhere at https://wiki.alliedmods.net/Category:SourceMod_Documentation
The lack of documentation of this behavior causes great frustration to newcomers. I had to find out about this by googling and finding old forum posts. Please update :)