Home > Enterprise >  what is the difference between const char * and const char ()[]
what is the difference between const char * and const char ()[]

Time:01-02

I found this function in some library that I would like to use but I can't figure out out to pass a std::string variable to it



----------


template<int KeyLen>
std::vector<unsigned char> key_from_string(const char (*key_str)[KeyLen]) {
    std::vector<unsigned char> key(KeyLen - 1);
    memcpy(&key[0], *key_str, KeyLen - 1);
    return key;
}

test_encryption.cpp

const std::vector<unsigned char> key2 = plusaes::key_from_string(usr_key.c_str()); 

this is how i am trying to call that function

Error

test_encryption.cpp: In function 'std::__cxx11::string encrypte_string(std::__cxx11::string, std::__cxx11::string)':
test_encryption.cpp:39:82: error: no matching function for call to 'key_from_string(const char*)'
  const std::vector<unsigned char> key2 = plusaes::key_from_string(usr_key.c_str());      // 16-char = 128-bit
                                                                                  ^
In file included from test_encryption.cpp:1:
pulse.hpp:685:35: note: candidate: 'std::vector<unsigned char> plusaes::key_from_string(const char (*)[17])'
 inline std::vector<unsigned char> key_from_string(const char (*key_str)[17]) {
                                   ^~~~~~~~~~~~~~~
pulse.hpp:685:35: note:   no known conversion for argument 1 from 'const char*' to 'const char (*)[17]'
pulse.hpp:690:35: note: candidate: 'std::vector<unsigned char> plusaes::key_from_string(const char (*)[25])'
 inline std::vector<unsigned char> key_from_string(const char (*key_str)[25]) {
                                   ^~~~~~~~~~~~~~~
pulse.hpp:690:35: note:   no known conversion for argument 1 from 'const char*' to 'const char (*)[25]'
pulse.hpp:695:35: note: candidate: 'std::vector<unsigned char> plusaes::key_from_string(const char (*)[33])'
 inline std::vector<unsigned char> key_from_string(const char (*key_str)[33]) {
                                   ^~~~~~~~~~~~~~~
pulse.hpp:695:35: note:   no known conversion for argument 1 from 'const char*' to 'const char (*)[33]'

CodePudding user response:

The function in the library only accepts pointers to string literals. Example:

std::vector<unsigned char> key = plusaes::key_from_string(&"Foo");

Here KeyLen would be deduced to 4 because the string literal consists of 'F', 'o', 'o', '\0'

You supply usr_key.c_str() to the function, which returns a const char*, and that's not a match to such a function. All template parameters must be known at compile time and KeyLen is such a parameter. It must be a compile time constant.

An alternative is to create the std::vector<unsigned char> manually:

std::vector<unsigned char> key2(usr_key.begin(), usr_key.end());

or if you want, create your own helper function:

std::vector<unsigned char> my_key_from_string(const std::string& s) {
    return {s.begin(), s.end()};
}

and use it like so:

auto key2 = my_key_from_string(usr_key);
  • Related