1

I create an Entity object:

#ifndef ENTITY_H  
#define ENTITY_H  

struct MyEntityObject {  
    char  _entityAuthor;
};  

#endif // ENTITY_H  

Then I try to set it up and use it:

struct MyEntityObject myEntityObject;  

/* MyEntityObject data specification */  
strcpy(myEntityObject . _entityAuthor, "Shakespear");  

I get this error:

main.cpp:37: error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]  
      myEntityObject . _entityAuthor = "Shakespear";  
                             ^

What is going on here? What am I getting wrong?

2 Answers 2

2

Your struct field is only a single char

struct MyEntityObject {  
    char  _entityAuthor;  
}; 

Trying changing it to either a char * that you dynamically allocate memory to or a char array if you can make solid assumptions about the size of the string.

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

1 Comment

Just to clarify the meaning of dynamically allocate above, if the OP uses a char * for the field, then the call to strcpy will crash. strdup will allocate the memory for the field, if malloc hasn't been called.
0

This line:

char _entityAuthor;

...declares a char variable.

What you need is a pointer to char (char *) variable:

char *_entityAuthor;

You will also need to allocate memory with malloc() before using strcpy().

Here is a tutorial.

Comments

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.