Reputation: 5972
I see in c++ programming language some recommendations like "don't use char in struct type",
struct Student{
int stdnum, FieldCode, age;
double average, marks, res[NumOfCourses];
char Fname[20], Lname[20], cmp[20];
};
And is better to use:
struct Student{
int stdnum, FieldCode, age;
double average, marks, res[NumOfCourses];
string Fname, Lname, cmp;
};
Any other recommendations would be welcome on the matter
Thank you in advance
Upvotes: 2
Views: 677
Reputation: 106236
For your student class, you have first name, last name and "cmp" - I've no idea what that's supposed to be. Clearly a student could have a first and/or last name longer than 20 characters, so by hardcoding an array of 20 elements you've already created a system that:
If you don't carefully check all your input handling when modifying arrays, your program could crash, corrupt data and/or malfunction.
With std::string
, you can generally just let people type into whatever fields your User Interface has, or pick up data from files, databases or the network, and store whatever you're given, print whatever you're given, add extra characters to it without worrying about crossing that threshold etc.. In extreme situations where you can't trust your input sources (e.g. accepting student data over untrusted network connections) you may still want to do some length and content checks, but you'd very rarely find it necessary to let those proliferate throughout all your code the way array-bounds checking often needs to.
There are some performance implications:
new[]
), and the allocation routines might allocate more memory than you actually asked for, or be unable to effectively reuse delete[]
d memory due to fragmentationstd::string
implementation happens to share a buffer between std::string
objects that are copied (just until one is or might be modified), then std::string
s could reduce your memory usage when there are multiple copies of the student struct
- but this probably only happens transiently and is only likely to help with very long stringsIt's hard to conclude much from just reading about all these factors - if you care about potential performance impact in your application, you should always benchmark with your actual data and usage.
Separately, std::string
is intuitive to use, supporting operators like +
, +=
, ==
, !=
, <
etc., so you're more likely to be able to write correct, concise code that's easily understood and maintained.
Upvotes: 2
Reputation: 400029
Because string handling using C-level raw character arrays is hard to get right, prone to fail, and when it fail it can fail rather horribly.
With a higher-level string datatype, your code becomes easier to write, more likely to be correct. It's also often easier to get shorter code, since the string
datatype does a lot of work for you that you otherwise have to do yourself.
The original question was tagged with the c tag, but of course string
is a c++ construct so this answer doesn't apply to C. In C, you can choose to use some string library (glib's GString is nice) to gain similar benefits but of course you'll never have overloaded operators like in C++.
Upvotes: 8
Reputation: 1617
@unwind's answer is generally perfectly appropriate, unless it's really a sequence of 20 char's you're after. In that case a std::string might be overkill (performance-wise as well), but a std::array might still be better.
Upvotes: 3
Reputation: 12658
I dont think it makes difference in C
as well.
Check this precious stackoverflow answer explained clearly states strings
are more secure than char *
.
Upvotes: 1