简体   繁体   中英

c++ Create Char Array from Variable String passed as function parameter

I want to create a char array in a function from a string I have passed in.

bool foo(string s1, string s2) {
    char s1_char_array[] = s1;
    char s2_char_array[] = s2;
}

But I am met with

 "Initialization with '{...}' expected for aggregate object."

For both declarations.

I've tried a lot of work arounds but they all have errors of their own. Learning C++ and every tutorial I find has the value hardcoded. How to make this work?

You can dynamically allocate arrays and copy data from the strings there.

#include <string>
#include <cstring> // for strcpy()
#include <algorithm> // for std::copy()
using std::string;

bool foo(string s1, string s2) {
    // allocate arrays (+1 for terminating null-characters)
    char* s1_char_array = new char[s1.size() + 1];
    char* s2_char_array = new char[s2.size() + 1];
    // copy data to arrays
#if 0
    // if the strings are guaranteed not to contain '\0'
    strcpy(s1_char_array, s1.c_str());
    strcpy(s2_char_array, s2.c_str());
#else
    // if the strings may contain '\0'
    std::copy(s1.begin(), s1.end(), s1_char_array);
    std::copy(s2.begin(), s2.end(), s2_char_array);
    s1_char_array[s1.size()] = '\0';
    s2_char_array[s2.size()] = '\0';
#endif
    
    // do things with the arrays

    // deallocate the arrays
    delete[] s1_char_array;
    delete[] s2_char_array;
    // return something
    return false;
}

The bad news is that there is no way to create an array variable from a std::string in general because std::string can represent strings of any size (very large practial limits exist but you'll run out of memory first on 64 bit systems) and that size is dynamic ie determined at run time. By contrast, the size of an array must be known at compile time at which time the size of the string is still unknown.

Another problem is that your arrays have automatic storage and the memory available for automatic storage is very limited - usually one to few megabytes on desktops / servers; potentially much less on embedded - while the dynamic memory owned by the string doesn't have such restriction and thus attempting to fit a large string into automatic storage could easily cause a "stack overflow".

Another issue is that arrays cannot be copy-constructed nor converted from other types so char s1_char_array[] = some_variable; can never work.


The good news is that you don't ever need to do that either. std::string already contains an array of char s internally, so there is no need to create a new array. Just keep using the array that is in the string.

this is what I come up with. you have to declare the char array size same as strings by using string.lenght() function and

then use loops to copy the strings elements to char array index by index

bool foo(string s1, string s2) {
    
    char s1_char_array[s1.length()] ;
    char s2_char_array[s2.length()] ;
    for(int i=0; i<s1.length(); i++){
        s1_char_array[i]=s1[i];
    }
    for(int i=0; i<s2.length(); i++){
        s2_char_array[i]=s1[i];
    }
}

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