Very simple question

Apr 20, 2013 at 12:42pm
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";
}
Apr 20, 2013 at 12:51pm
Arrays have no the assignment operator. You should use C function strcpy.

#include <string.h>

strcpy( example->maker, "Stark Industries" );
Apr 20, 2013 at 12:52pm
ok, thanks ;)
Apr 20, 2013 at 1:17pm
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.