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

Constant arrays #2129

Open
6 tasks done
dronelektron opened this issue Mar 26, 2024 · 4 comments
Open
6 tasks done

Constant arrays #2129

dronelektron opened this issue Mar 26, 2024 · 4 comments

Comments

@dronelektron
Copy link

Help us help you

  • I have checked that my issue doesn't exist yet.
  • I have tried my absolute best to reduce the problem-space and have provided the absolute smallest test-case possible.
  • I can always reproduce the issue with the provided description below.

Environment

  • Operating System version: Windows 10 Pro
  • Game/AppID (with version if applicable): 300
  • Current SourceMod version: 1.11.0.6943
  • Current SourceMod snapshot: 1.12.0.7114
  • Current Metamod: Source snapshot: 1.11.0-dev+1153

Description

The const modifier doesn't work for arrays

Problematic Code (or Steps to Reproduce)

File: constants.sp

#include <sourcemod>

const bool boolValue = true;
const char charValue = 'a';
const int intValue = 1;
const float floatValue = 1.0;

const bool boolArray[] = {true, false, true};
const char charArray[] = {'a', 'n', 'd'};
const int intArray[] = {1, 2, 3};
const float floatArray[] = {1.0, 2.0, 3.0};

public Plugin myinfo = {
    name = "",
    author = "",
    description = "",
    version = "",
    url = ""
};

Logs

$ spcomp constants.sp
SourcePawn Compiler 1.12.0.7114
Copyright (c) 1997-2006 ITB CompuPhase
Copyright (c) 2004-2021 AlliedModders LLC

constants.sp(8) : error 001: expected token: "=", but found "["
     8 | const bool boolArray[] = {true, false, true};
--------------------^

constants.sp(9) : error 001: expected token: "=", but found "["
     9 | const char charArray[] = {'a', 'n', 'd'};
--------------------^

constants.sp(10) : error 001: expected token: "=", but found "["
    10 | const int intArray[] = {1, 2, 3};
-------------------^

constants.sp(11) : error 001: expected token: "=", but found "["
    11 | const float floatArray[] = {1.0, 2.0, 3.0};
---------------------^

4 Errors.
@MAGNAT2645
Copy link
Contributor

MAGNAT2645 commented Mar 26, 2024

It should work if you add static or public before const.

@dronelektron
Copy link
Author

dronelektron commented Mar 26, 2024

Strange, but this actually works, except in the case of charArray. Why can't you just use the const modifier? Why would I hide a constant using static or make it visible to other plugins using public?

@dvander
Copy link
Member

dvander commented Mar 26, 2024 via email

@dronelektron
Copy link
Author

dronelektron commented Mar 26, 2024

Yea, I see...

static const char test[] = "Candy";

public void OnPluginStart() {
    MakeSandy(test);
    PrintToServer("[DEBUG] test = '%s'", test);
}

void MakeSandy(const char[] str) {
    str[0] = 'S';
}

Terminal:

$ spcomp constants.sp
SourcePawn Compiler 1.11.0.6943
Copyright (c) 1997-2006 ITB CompuPhase
Copyright (c) 2004-2021 AlliedModders LLC

Code size:         3636 bytes
Data size:         2348 bytes
Stack/heap size:      16520 bytes
Total requirements:   22504 bytes

Server console:

[DEBUG] test = 'Sandy'

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

3 participants