0

I want to initialize a local char array with a string, which is generated to a static const pointer. Basically it looks like this:

static const char * const FOO = "foo"; /* generated */
char bar[12] = FOO;                    /* my code */

The compiler does not accept it:

error: array must be initialized with a brace-enclosed initializer

What construct can I use to initialize the char array bar with the string pointed to by FOO?

Context: in my company, we write unit testers for C code using a C++ framework. Therefore the bar parameter must be an array and cannot be a C++ string type. The FOO constant is generated from a proprietary IDL. The code generator generates a #define for C code, but a static const char * const for C++.

8
  • 7
    char bar[12]; strcpy(bar, FOO);? Commented Jan 30, 2020 at 12:08
  • 1
    Do you need to modify the string at all? If not, why not just stay with the pointer? Commented Jan 30, 2020 at 12:08
  • @Aconcagua: yes the string must be modified later. Commented Jan 30, 2020 at 12:09
  • @mch and Thomas Sablik: your solutions would work, but if possible I'd like a one-line solution. Commented Jan 30, 2020 at 12:10
  • I would add Assert(sizeof bar > strlen(FOO)); to the test code. And thereafter strcpy(bar, FOO);. This makes your tester more robust agains changes in the generated code. Commented Jan 30, 2020 at 12:15

4 Answers 4

3

You cannot initialise the array with the pointer. But you can copy the string after default-initialising the array.

in my company, we write unit testers for C code using a C++ framework. Therefore the bar parameter must be an array and cannot be a C++ string type.

I don't follow your reasoning. This seems like a mistaken assumption.

Sign up to request clarification or add additional context in comments.

6 Comments

The bar variable is used as input for a C function under test that takes a char array as parameter.
@slingeraap That's what the c_str member function is for.
The parameter is changed by the function and c_str returns a const type.
@slingeraap In that case you can use data member function (since C++17; In older standards you can use &bar[0]).
But we still need C++11, before, std::string should be out...
|
2

I expect that the following would work:

static const char * const FOO = "foo"; /* generated */
char bar[12], *pBar = strcpy(bar, FOO);                    /* my code */

and then you could either access it via bar[] or through pBar.

There is a redundancy of the extra char * but it may be that it doesn't really matter.

You might even consider something like the following to guard against buffer overflow:

static const char * const FOO = "foo"; /* generated */
char bar[12] = {0}, *pBar = strncpy(bar, FOO, sizeof(bar)/sizeof(bar[0]) - 1);                    /* my code */

You may also consider wrapping this in a #define as in:

#define MAKEARRAY(name, size, thing) char name[size] = {0}, *p##name = strncpy(name, (thing), (size) - 1)

and then using it like:

MAKEARRAY(bar2, 14, FOO);

Comments

0

This works but IMHO looks ugly:

static const char *const X = "abc";
char x[12] = { X[0], X[1], X[2], X[3] };

Defines would work:

#define X "abc"
char x[12] = X;

Comments

0

This isn't ideal but should give you an idea. It uses local variables so they won't persist beyond these functions. I'm not sure you were using Static how you thought it should be used. I'd recommend NOT putting it in the function since you can't really access it outside of the function, but it will remain in memory.

void ModifyString(char* data, int length)
{
    std::cout << "Data: " << data << ", Length: " << length;
}

void ModifyString(const char* data)
{
    char buffer[256];
    int length = -1;
    while (data[++length])
        buffer[length] = data[length];
    buffer[length] = 0;
    ModifyString(buffer, length);
}

int main() 
{
    ModifyString("Hello Test");
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.