Thread: Compare pointer to struct and struct

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    17

    Lightbulb Compare pointer to struct and struct

    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!

  2. #2
    Nasal Demon Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    179
    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?

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    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:
    Code:
    int CompareEmployeeName(struct employee *lhs, struct employee *rhs)
    {
        return strcmp(lhs->name, rhs->name);
    }
    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.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    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.
    Quote Originally Posted by whiteflags View Post
    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:
    Code:
    int CompareEmployeeName(struct employee *lhs, struct employee *rhs)
    {
        return strcmp(lhs->name, rhs->name);
    }
    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.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct compare
    By John5 in forum C Programming
    Replies: 7
    Last Post: 03-06-2016, 07:12 PM
  2. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  3. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 05:38 PM
  4. Replies: 1
    Last Post: 05-05-2004, 06:58 AM
  5. returning a pointer of a struct of a struct array...
    By myrddinb in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 06:49 PM

Tags for this Thread