简体   繁体   中英

How to initialize a char array from a char pointer in C++?

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++.

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.

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);

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;

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM