tags:

views:

73

answers:

4

I ended up doing like this,

struct init
{
    CHAR Name[65];
};

void main()
{
    init i;

    char* _Name = "Name";

    int _int = 0;

    while (_Name[_int] != NULL)
    {
        i.Name[_int] = _Name[_int];
        _int++;
    }
}
+2  A: 

In C++, a struct can have a constructor, just like a class. Move the initialization code to the constructor. Also consider using a std::string instead of the char array.

struct init
{
    std::string name;

    init (const std::string &n) : name (n)
    {
    }
};
Vijay Mathew
I wish i could but it's one of the defined API structure i was working with.
Dave18
@Dave17 Why couldn't you have made that clear in your question? Why do we always have to prise information out of you?
anon
i'm sorry, won't happen.
Dave18
+2  A: 

You could also use strcpy() to copy your string data into the char array.

strcpy(i.Name, "Name");
xan
+5  A: 

Give your structure a constructor:

struct init
{
  char Name[65];
  init( const char * s ) {
     strcpy( Name, s );
  }
};

Now you can say:

init it( "fred" );

Even without a constructor, you can initialise it:

init it = { "fred" };
anon
Upvoted but I'd be happier with strncpy :)
Manuel
+1, I liked the idea of constructor.
Dave18
A: 

but what is an issue in this... any function exposed will do the same functionality to achieve copy chars of strings.

Kasma