I'm sorry to say that C++ is a bit unintuitive here. You can say:
char name[10] = "abcd";
and, given your definition above:
buffer buf = { "NDS", "3174" };
The latter relies on a one-to-one correspondence between fields in the structure and values in the list, so I've had to reverse the order used in your assignments.
But, you can't do your
buf.ProjectName = "abcde";
What that actually requests in C++ is that buf.ProjectName be loaded with a pointer to memory containing the character data "abcde". You can't do that though, as ProjectName itself is another buffer for character data, and not of pointer-to-character-data.
So, when you have a source and destination area containing NUL-terminated strings (Google ASCIIZ if necessary), you need to use a support function to copy from one to the other:
strcpy(buf.ProjectName, "name");
If ProjectName's dimension is too small, then your string may overwrite memory that the compiler hasn't reserved for ProjectName, probably causing a crash or erroneous output. You can protect against this - if the relative sizes of the strings isn't obviously ok - using strncpy(buf.ProjectName, "name", sizeof buf.ProjectName)
. Unfortunately, this means buf.ProjectName
may not hold the full expected value, making it of dubious use.
C++ improves over this way of handling textual data - which is inherited from C - with the std::string class. You can simple do this:
#include <string>
struct Buffer
{
std::string project_name_;
std::string project_id_;
};
Buffer b;
b.project_name_ = "abcde"; // works with string literals.
b.project_id_ = b.project_name_; // can copy from std::string to std::string