Structure and char array question

So i was experimenting with structure arrays today just to kind of review a little. ( i'm fairly new to c++ ) Anyway, I made an array of structures and initialized two of it's elements ( did i say that right? ). I wanted to see if i could modify an element of the array after initialization and i don't think i did it right. So that's kind of the first thing i need help on, various ways to modify the elements.

Then the next question i have is when i tried to do:
collection[2].title = "one piece";
I realized that i didn't know how to assign a string to a char array other then how you would do it with an initialization. ( or the cin input )
like i knew this worked:
char RandomArray[20] = "Hi!"
but what if i declared it and then wanted to modify the char array? would i have to assign a single character to each array element separately? There has to be a better way than that right? ( i know i can use the string object but i want to make sure i have arrays down good)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//random.cpp -- review

#include <iostream>

struct manga 
{
	char title[20];
	int volume;
};

int main()
{
	using namespace std;

	manga collection[10] =
	{
		{"D.grayman" , 1 },
		{"Fairy tail", 1}
	};

	cout<<"Title: "<<collection[0].title
	<<" , Volume #: "<<collection[0].volume<<endl;

	//First thing i have a question on:
	collection[2] {"One piece", 1} ;


	char test[20]="Hello!";
	//Second thing i want to know
	char test2[20];
	test2 = "How are you?";

	cin.get();
	return 0;
}
Last edited on
std::strcpy( collection[2].title,"One piece" ) ;
collection[2].volume = 1;



std::strcpy( test2, "How are you?" );


EDIT: Of course you need to include header <cstring>
Last edited on
Okay, thanks, that helps a lot! But one more thing, visual studio is telling me that strcpy is ambiguous and i should use strcpy_s instead, what is that all about?
It is simply a warning. You can disable it. Function strcpy_s is not a standard C++ function though it was adopted in the C Standard.
Last edited on
Topic archived. No new replies allowed.