Thread: assign null value to a struct and check if it is null

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18

    assign null value to a struct and check if it is null

    Hi everyone,

    I want to initialize my struct with a null value, and at some point check whether the struct is null.

    Code:
    struct mystruct s = {0};
    and then when i need to check it:
    Code:
    if(s == {0}) then do something
    but it doesn't seem to be the right way of doing it.

    Any help?

    Thanks in advance.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Perhaps you want to create a pointer to an object of your struct type, then initialise it to be a null pointer?

    Otherwise, you simply have to define what it means for your struct to be "null", do so, and thus it shall be "null".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Wrong code below. Please ignore.
    I just keep it here because there have been comments based on it.

    Code:
    struct mystruct s = {0}, snull = {0};
    /* possibly change s */
    /* WRONG CODE BELOW, SORRY */
    if (s == snull) { /* do something */ }
    Last edited by qny; 10-30-2012 at 10:08 AM. Reason: wrong code

  4. #4
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18
    Hi qny,

    When I tried to do that, it gave me "invalid operand to binary ==" error..

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Just compare the members of the struct with 0. The use of memcmp may be an appropriate alternative. As I said, if you really do want to talk about a "null" struct, then you need to define precisely what that means. As yet, you have not even shown us your struct definition.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Quote Originally Posted by ymc1g11 View Post
    Hi qny,

    When I tried to do that, it gave me "invalid operand to binary ==" error..
    Oops, my bad. Sorry.
    You need to compare member by member (for those members that are comparable)
    Code:
    if ((s.member1 == snull.member1) &&
        (s.member2 == snull.member2) &&
    /*  ...  */
        (s.memberN == snull.memberN)) { /* do something */ }

  7. #7
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18
    Quote Originally Posted by laserlight View Post
    Just compare the members of the struct with 0. The use of memcmp may be an appropriate alternative. As I said, if you really do want to talk about a "null" struct, then you need to define precisely what that means. As yet, you have not even shown us your struct definition.
    laserlight,

    here's the struct i created:
    Code:
    struct tnode {
    	char *word;
    	int count;
    	struct tnode *left;
    	struct tnode *right;
    };
    I am trying to implement it as a tree. So, to do that, I initialized the root of the tree as NULL as follows:
    Code:
    struct tnode root = {0};
    So every time when I add an element(which is a char*), it will check if the current node is null, add the element to root if yes, else traverse the tree recursively.

    Much thanks in advance

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    The thing is, the root is not "null". It is zero initialised.

    It might make more sense to have root be a pointer instead. If you do want it to be a struct tnode object, then you can either compare its members with 0, or just the word member.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    root should be a pointer just like left and right are. Anything else would honestly be a mistake.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18
    Quote Originally Posted by laserlight View Post
    The thing is, the root is not "null". It is zero initialised.

    It might make more sense to have root be a pointer instead. If you do want it to be a struct tnode object, then you can either compare its members with 0, or just the word member.
    sorry i just cant get my head round it as a beginner

    so i should declare it in my main as this:
    Code:
    struct tnode *root;
    and in my method sort,
    -how do i check if the 'root' had been assigned anything?
    -do i need to first declare the 'root' is empty?

    this is my method which sort words into the tree (made of struct tnode):
    Code:
    void sort(struct tnode **r, char *w){    
        if(r == null){
            r = (struct tnode *) malloc(sizeof(struct tnode));
            r->word = w;
            r->count = 1;
            r->*left = null;
            r->*right = null;
            (printf("%s\n", r->word));
        }else{
        
            int cmp = strcmp(w, r->word);
            if(cmp<0){
                sort(r->left, w);
            }else if(cmp>0){
                sort(r->right, w);
            }else r->count++;
            
        }
    }
    Please help~ and much thanks~!

  11. #11
    Registered User
    Join Date
    Oct 2012
    Location
    Southampton, United Kingdom.
    Posts
    18
    sorted! cheers!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. check null structs
    By jessCPP in forum C Programming
    Replies: 3
    Last Post: 01-04-2012, 04:47 AM
  2. Check if string is null
    By doubty in forum C Programming
    Replies: 3
    Last Post: 07-04-2009, 03:10 PM
  3. Assign null to a character array.
    By mahedee in forum C Programming
    Replies: 1
    Last Post: 08-06-2008, 07:16 AM
  4. accept(ListenSocket, NULL, NULL); cause program to hang?
    By draggy in forum Networking/Device Communication
    Replies: 11
    Last Post: 06-16-2006, 03:40 PM
  5. Check if a cin is null
    By HumbuckeR in forum C++ Programming
    Replies: 6
    Last Post: 04-16-2006, 08:16 AM