0

Complete Beginner in C++.

This is a member initialization list:

Student.cpp

Student::Student(int studentID ,char studentName[40]) : id(studentID), name(studentName){};

Student.h

class Student{

protected:
    char name[40];
    int id;
}

My problem is that name is of type char[40], so, name(studentName) displays an error of:

a value of type "char *" cannot be used to initialize an entity of type "char [40]"

How can I initialize name array, to studentName array in the member initializer list? I don't want to use string, and I have tried strcpy and didn't work

6
  • 7
    Use std::array instead or even better std::string Commented May 30, 2015 at 22:16
  • @101010 I don't want to use string as I am using a binary file Commented May 30, 2015 at 22:19
  • 6
    What has a binary file to do with it? Commented May 30, 2015 at 22:20
  • @EdHeal In other parts of the program , I am using the overloaded constructor to write to a binary file, and strings are causing me a lot of trouble when reading them, so I just want to know how I can initialize the name array. I am trying to grasp the std::array Commented May 30, 2015 at 22:23
  • @PianomaR, std::string stores bytes. Whatever is going wrong with reading the file would be the same with character arrays. Commented May 30, 2015 at 22:27

2 Answers 2

4

Since you can't initialize (raw) arrays with other arrays or even assign arrays in C++, you basically have two possibilities:

  1. The idiomatic C++ way would be to use std::string, and the task becomes trivial:

    class Student{
    public:
        Student(int studentID, const std::string& studentName)
        : id(studentID), name(studentName) {}
    protected:
        std::string name;
        int id;
    };
    

    Then, when needed, you can get the underlying raw char array from name by calling the c_str member function:

    const char* CStringName = name.c_str();
    
  2. If you want to use a char array instead, things get more complicated. You can first default-initialize the array and fill it in the constructor body with strcpy:

    class Student{
    public:
        Student(int studentID, const char* studentName)
        : id(studentID) {
            assert(strlen(studentName) < 40); // make sure the given string fits in the array
            strcpy(name, studentName);
        }
    protected:
        char name[40];
        int id;
    };
    

    Note that the argument char* studentName is identical to char studentName[40], since you can't pass arrays as parameters by value, which is why the compiler just treats it as a char* pointing to the first char in the array.

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

1 Comment

(2) calculates studentName length twice, and it should be const char*
1

You cannot implicitly copy arrays, they just do not have this feature. Here is what you can do instead:

The best option you have is safe the name in a std::string instead of an char[]. This would work just as your example, but could handle names of arbitrary length.

Another alternative would be std::array<char, 40>. This is nearly the same as the char[] you use right now, but has the advantage of being copyable. It also would work with the code you showed. Unlike the string option, this would be trivially copyable, i.e. you can for example send and receive this as binary data.

If you really want or need to use the char[], you need to copy over the string "by hand":

Student::Student(int studentID ,char studentName[40]) : id(studentID){
    std::strcpy(name, studentName);
}

4 Comments

I really need to user char[] so I am copying the string "by hand" Now it shows the error: "strcpy" is not a nonstatic data member or base class of class "Student"
@PianomaR Did you #include <cstring>?
Yes, I have included #include <cstring> and #include <array>
@PianomaR Oh, had a typo. Thanks Zenith. Also have a look at the error handling Zenith showed if you do not control the input (or assert anyway, it does not hurt). Also the only reason to not use std::array here were if someone would forbid you to.

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.