0

i have a class which has several members of char arrays, and i want to initialize this class with an array of strings which are the values of the char arrays.

class Product {
public:
    char productId[20];
    char productName[50];
    char price[9];
    char stock[9];

    Product(vector<string> v) : productId(v[0]), productName(v[1]), price(v[2]), stock(v[3]) {  }
};

with this code i get an error that say no suitable conversion function from "str::string" to "char[20]" exist

5
  • 2
    You will need to strcpy into those arrays. Commented Dec 9, 2021 at 20:37
  • ^ after checking the length will fit Commented Dec 9, 2021 at 20:40
  • Have you thought about what should happen if a std::string is too long to fit into its fixed character array? Commented Dec 9, 2021 at 20:40
  • At least pass the vector by reference so you don't invoke unnecessary allocations and additional copies. If you can't guarantee safe string lengths at runtime to avoid overflowing these, consider strncpy_s. Commented Dec 9, 2021 at 20:41
  • thank you all for your answers, first i have no priority with memory safety or stuff like that, i'm a new learner of c++ and i'm stuck with this. Second i've tried with strcpy but i got an error like this: a value of type "char *" cannot be used to initialize an entity of type "char [20] Commented Dec 9, 2021 at 20:45

1 Answer 1

1

The code at the bottom will work. But is this a good idea? Probably not. You are better of just storing std::string in your Product type directly:

#include <cassert>
#include <iostream>
#include <vector>

#include <string.h>

class Product {
public:
  std::string productId;
  ...

  Product(std::vector<std::string> v) : productId{std::move(v[0])} {}
};

There is a problem with this code though; where do you check the vector has all required elements? Better to make an interface that specifies the four strings a Product is made up of separately:

class Product {
public:
  std::string productId;
  ...

  Product(std::string pid, ...) : productId{std::move(pid)}, ... {}
};

But in case you insist on a C/C++ amalgamation;

#include <cassert>
#include <vector>

#include <string.h> // strcpy

class Product {
public:
  char productId[20];
  char productName[50];
  char price[9];
  char stock[9];

  Product(const std::vector<std::string> &v) {
    assert(!v.empty()); // really; v.size() == 4
    ::strncpy(productId, v[0].c_str(), 19);
    productId[19] = '\0';
    // ...etc.
  }
};
Sign up to request clarification or add additional context in comments.

2 Comments

In your last example, you should use (std::)strncpy() instead to avoid potential buffer overflows, and should ensure the data is actually null-terminated, eg: strncpy(productId, v[0].c_str(), 19); productId[19] = '\0';
@RemyLebeau, yes, I've updated the answer.

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.