Very simple question

I try to access a char array in a structure through a pointer-to-structure. But it comes with this error when compiling:
incompatible types in assignment of `const char[16]' to `char[40]


1
2
3
4
5
6
7
8
9
10
11
struct box{
char maker[40];
float height;
float width;
float length;
float volume;
};
main(){
box* example = new box;
example->maker = "Stark Industries";
}
Arrays have no the assignment operator. You should use C function strcpy.

#include <string.h>

strcpy( example->maker, "Stark Industries" );
ok, thanks ;)
You may want to look into std::string, Minimacfox.
Then you could do the assignment operation like you originally tried.
http://cplusplus.com/reference/string/

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <string>

struct box {
    std::string maker;
    // ...
};

int main() {
    box* example = new box;

    example->maker = "Stark Industries";
    delete example; // in C++, every "new" must have a corresponding "delete"
}
Topic archived. No new replies allowed.