some clarification
string is a standard c++ library class which internally manipulates a array of chars, just like the one you declared "char name[]".
using directly an char array brings lots of constraints. And to manipulate it you have to use functions that iterate through out the array and apply the needed changes.
string is a class that internally does all that for you, and provides a simpler interface, so that you can manipulate it like any primitive variable.
About the struct - here's a clear explanation
Whenever you want to use a int what do you do ? what is a int ?
Code:
int y = 0;
y = y+2;
...
in that declaration you mention that y will be some variable that you'll manipulate. That variable is stored somewhere in memory. It doesn't mind.
int is the type of that variable. You're telling that y will be a integer. I could be a char, a float, some other type. You just have to choose.
Code:
struct company{
... //contents - don't really matter
};
in that declaration you're stating that company is the name of a structure. company will then be associated with a type of structures which was defined together with de declaration of struct company{}... In other words company will be a user defined type, as a short hand to the contents of the struct.
Then, like the instantiatoin of int y. you'll want to create an instance of that structure to manipulate it.
Code:
struct company x;
int y;
now you're saying that x will be a variable to manipulate, just like y. The type of x is that of your defined structure, just like the type of y is a int.
so now you have an instance of your struct
you can create multiple instances of the same struct, each being independant one from another
Code:
struct company x, y;
struct company w;
now you want to fill the struct with something
Code:
struct company{
char name[50];
int sales;
};
struct company x;
if x is a instance of the structure, you will have instances of the structure variables within it. to acess each of the variables use the '.' operator
Code:
struct company x;
strcpy(x.name, "big deal" );
s.sales = 50;
when acessing a variable with the '.' operator just treat it normally as you would treat any variable of that type, like not being inside a struct
to end, you may want to create a instance of a struct with already defined values for each contained variable, or in other words, initialization values for each variable.
considering your struct
Code:
struct company {
char name[50];
int sales;
int price;
char owner[50];
};
struct company x = {
"big deal" , //field name, which is char[]
1000, //field sales which is a int
50000, //field price which is a int
"mister lazy" //field owner which is char[]
};
after that declaration, each of the structs contained variables will have those assigned values. And note that the order of the arguments for the initialization, is the same of the declaration of the struct
a structure simply holds a defined amount of variables to the programmers liking.