Reputation: 140
I'm begginer in programming and C++.
Programming on Linux, I am creating a typedef struct as below:
typedef struct {
char id[10];
string name[20];
}Employee;
Usually, when not using struct, I initialize string char doing:
char id[10]="ID02093";
string name="Joe";
So, why can't I initialize char id and string name of the Employee struct as below?:
Employee salesManager;
salesManager.id="ID02093";
salesManager.name="Joe";
I'm getting this errors from the g++ compiler:
"incompatible types in assignment of ‘const char [8]’ to ‘char [9]’ salesManager.id="ID02093";" incompatible types in assignment of ‘const char [4]’ to ‘std::__cxx11::string [20] {aka std::__cxx11::basic_string [20]}’ salesManager.name="Joe";
What's the right way to initialize string and char of typedef struct elements out of the typedef struct declaration?
Upvotes: 0
Views: 2116
Reputation: 70392
char
Considering this line:
salesManager.id="ID02093";
You are allowed to initialize an array of char
with a string literal as a convenience allowed by the syntax C++ inherited from C. However, you are not permitted to use assignment with an array. Which is why you got this error:
incompatible types in assignment of ‘const char [8]’ to ‘char [9]’ salesManager.id="ID02093";
Instead, you have to copy the data into the array.
string
Considering this line
salesManager.name="Joe";
The reason this failed is because you had declared name
to be an array as well.
typedef struct { char id[10]; string name[20]; }Employee;
This explains why you got this error:
incompatible types in assignment of ‘const char [4]’ to ‘std::__cxx11::string [20] {aka std::__cxx11::basic_string [20]}’ salesManager.name="Joe";
You likely did not mean to declare name
to be an array.
A typedef
name given to an anonymous class type has slightly different semantics from a class name. Since the differences are tricky, it is best to avoid using anonymous classes if it is not contained within a class itself.
Upvotes: 0
Reputation: 263217
Initialization and assignment are two distinct (but similar) things.
You can initialize an array by providing a value for it when you declare/define it.
You cannot assign to an array.
You can copy a value into an array using memcpy
or strcpy
:
char name[20];
strcpy(name, "Test";
But for your purposes, you can initialize the array as part of the initialization of the structure:
typedef struct {
char id[10];
char name[20];
} Employee;
Employee salesManager = {
"A12345678",
"Test"
};
/* OR */
Employee salesManager = {
.id = "A12345678",
.name = "Test"
};
Since you're asking about C++ rather than C, it's probably better to use std::string
rather than raw character arrays. std::string
is a lot more flexible: you can assign values, including string literals, to std::string
objects.
Upvotes: 4
Reputation: 863
I think you should use string in both cases, and use OOP instead of typedef. But try this code bellow, should work fine:
#include <iostream>
using namespace std;
typedef struct {
string id;
string name;
} Employee;
int main() {
Employee salesManager;
salesManager.id = "A12345678";
salesManager.name = "Test";
cout << "Id: " << salesManager.id << endl;
cout << "Name: " << salesManager.name << endl;
return 0;
}
Upvotes: 0