Thread: Question related to NULL assignment in pointers to structs

  1. #16
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by rstanley View Post
    Typo, obviously! "Good" not "god"! ;^)

    Although, some programmers do think C is a religion! ;^)
    Side note: the two words actually come from the same root. To be a "good man" for example used to mean a "godly man". Of course the meanings have obviously diverged quite a bit over the millennia. (As in, "He was a good thief"!)

    Back to the topic, I almost always initialize variables (global or otherwise) regardless. For me it is simply a matter of style, really, even though the standard does basically guarantee that globals will zero-initialized. That said, there are some instances where I do indeed omit the initialization:

    Code:
    int ch;
    for (;;) {
      ch = getchar();
      if(ch == EOF)
        break;
      // do something with 'ch' 
    }
    I think the fact the declaration is "within view" of where it is being used is why I don't have any issues with that. As long as it is obvious that the variable in question is "decidedly uninitialized", why not?

  2. #17
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    the two words ["good" and "god"] actually come from the same root
    Etymologically, the words "good" and "god" are unrelated.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #18
    Registered User Sir Galahad's Avatar
    Join Date
    Nov 2016
    Location
    The Round Table
    Posts
    277
    Quote Originally Posted by john.c View Post
    Etymologically, the words "good" and "god" are unrelated.
    It is the Norse language from whence these words arise (as do the vast number of words used everyday in English). In that context to be aligned with the "godspirit" in one's actions was the whole intention of "being good". Unfortunately the Norse were not very keen to record much of anything, nevermind etymological information. The knowledge however has not really been lost. It is still right in front of us, albeit unwritten.

  4. #19
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by Sir Galahad View Post
    Side note: the two words actually come from the same root. To be a "good man" for example used to mean a "godly man". Of course the meanings have obviously diverged quite a bit over the millennia. (As in, "He was a good thief"!)

    Back to the topic, I almost always initialize variables (global or otherwise) regardless. For me it is simply a matter of style, really, even though the standard does basically guarantee that globals will zero-initialized. That said, there are some instances where I do indeed omit the initialization:

    Code:
    int ch;
    for (;;) {
      ch = getchar();
      if(ch == EOF)
        break;
      // do something with 'ch' 
    }
    I think the fact the declaration is "within view" of where it is being used is why I don't have any issues with that. As long as it is obvious that the variable in question is "decidedly uninitialized", why not?
    Yes, in this very simple example, the initialization is not needed. But in real life and actual programs, it might be too easy for the author, or maintainer when adding or changing code, to use an uninitialized variable, assuming the variable has a valid value.

    If all programmers, especially beginning programmers, initialize ALL variable definitions, both global and especially local variables, then possible errors are greatly reduced!

  5. #20
    Registered User
    Join Date
    Dec 2023
    Posts
    6
    When you identify a pointer as NULL, this means there's no instantiation of a struct of the type node that you have just defined associated with it yet. There is no struct of the type node in memory that it points to.

    You can attach a struct of the type node to the pointer, for instance:

    Code:
    struct node
    {
      int value;
      struct node *next;
    };
    
    node* first;
    node myIntEnum;
    myIntEnum.value = 20;
    first = &myIntEnum;
    But until you do so, there is no pointer to the next element in the enumeration. Once you have your first element of the enumeration - say "node firstNode;" - when you don't have your second yet, the value of firstNode.next will be NULL.

    Once you have defined your type "node" as a "struct," I don't think you have to keep repeating that a node is a struct in declaring variables of the type node. You have to look this up to be sure, but once you have defined the type "node" you should just be able to call it just a node.

    Also look into type definitions in C in terms of defining your own types.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another pointers / struct/ array related question
    By patishi in forum C Programming
    Replies: 3
    Last Post: 09-11-2013, 05:25 PM
  2. a question on pointers, structs and arrays
    By onefootswill in forum C Programming
    Replies: 3
    Last Post: 12-06-2007, 01:27 AM
  3. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  4. Question related to array and pointers
    By Q4u in forum C++ Programming
    Replies: 6
    Last Post: 07-26-2002, 12:54 PM
  5. Pointers Question.....Null Pointers!!!!
    By incognito in forum C++ Programming
    Replies: 5
    Last Post: 12-28-2001, 11:13 PM

Tags for this Thread