arraysconst-char

Creating const char arrays in C++


so im trying to create an array with const char everytime the function is called i want to add to my array like a strtok

so if i to wanted set

struct R2R
{
private:
    int Index;

    const char* DvarA[10000];
    const char* DValueA[100000];

public:
    void AddInfDvar( int DvarCount ,const char* Dvar, const char* Value)
    {
        setClientDvar(Dvar, Value);
        DvarA[DvarCount] = Dvar;
        DValueA[DvarCount] = Value;

    }
}R2R;

so if i called it like this

DvarA[1] = "Test";

GetDvar(int Num)
{
 return DvarA[Num];
}

would it return Test?

i just want to make sure im doing it right


Solution

  • The code you've set up creates an array of 10,000 const char*s, each of which can be thought of as a pointer to an immutable C-style string. Therefore, if you write

    DvarA[1] = "Test";
    

    you're storing a pointer to the string literal "Test" into slot 1 in the array. If you then read it back later, you will indeed get back "Test."