I'm trying to pass various string
s into members of a Struct
via pointer
but I am doing something fundamentally incorrect. What I think is that it doesn't need to be dereferenced. The process below works for other types of data such as int
or char
. For example:
typedef struct Course {
string location[15];
string course[20];
string title[40];
string prof[40];
string focus[10];
int credit;
int CRN;
int section;
} Course;
void c_SetLocation(Course *d, string location){
d->location = location;
. . .
}
I get an error when I try to compile the following algorithm to initialize a Course
:
void c_Init(Course *d, string &location, ... ){
c_SetLocation(d, location[]);
. . .
}
The error:
error: cannot convert ‘const char*’ to ‘std::string* {aka std::basic_string<char>*}’ for argument ‘2’ to ‘void c_Init(Course*, std::string*, ..
string location[15]
means that you want to create 15 instances of astring
, and each of those individual instances can have any length of text.Instead of
d->location
, you need to assign one of those 15 strings:d->location[0] = location
,d->location[1] = location
, etc.You're actually defining, for example, an array of 15 strings in the
location
field. Either use regular strings; e. g.:or use char arrays:
When you declare
char a[10]
, you're creating an array of 10 characters. When you declare anstd::string
, you're creating a string that can grow to arbitrary size. When you declare anstd::string[15]
, you're creating an array of 15 strings that can grow to arbitrary size.Here's what your struct should look like: