Dear everyone,
I gonna choose either pointer to struct or struct to use for my program (group data). So can you help to explain for me which one I should you or give me some suggestion.
Thank you!
Printable View
Dear everyone,
I gonna choose either pointer to struct or struct to use for my program (group data). So can you help to explain for me which one I should you or give me some suggestion.
Thank you!
What do you mean - either? If you use a pointer - you still have to have the struct it points to. ;)
Can you elaborate on your question, though?
I would assume that comparing structures is what you want to do, because that has more meaning. If you have a field "name" for example that you want to compare, it is much more meaningful to write a function like:
Yes, the function takes pointers to structures as arguments, but that is for another reason, pointers are fast for the computer to copy into the function stack. It would work just as well if the arguments were plain structures.Code:int CompareEmployeeName(struct employee *lhs, struct employee *rhs)
{
return strcmp(lhs->name, rhs->name);
}
As Xupicor says, please elaborate. What's the specific case where you can use either?
Generally, using pointers has a fixed performance cost of de-referencing the pointer. Using a value directly has a cost of making a copy, the cost of which depends on what the struct contains, it's size, and if it has a complex copy constructor.
While this is decent C, this is the C++ forum. As such this would be better written as a get_name() method in employee, that returns an std::string.