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!
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?
If you want people to help you, try to provide Short, Self Contained, Correct (Compilable), Example, and Don't be a Help Vampire!
C++ Super-FAQ - CppCon videos - C++14 latest draft (n3797) - C11 latest draft (n1570)
Boost - GCC 6.1.0 - Clang - GDB tutorial - Valgrind - Programming in 21 days
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.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.