Thread: Question related to NULL assignment in pointers to structs

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by C_me_run
    The assignment here doesn't appear to be necessary, as I've tested pointers to struct without the NULL assignment
    That's initialisation, not assignment. The key here is that you have declared these variables outside of a function, so they have static storage duration (this is true even though the static keyword was not used; if you did use it, they would still have static storage duration, but would have internal linkage instead of the external linkage that they have now), and are initialised at startup. If you don't specify any constant for initialisation, they are zero initialised, which for pointers means that they are initialised to be null pointers.

    Quote Originally Posted by C_me_run
    and when you make the linked list...the next pointer is NULL by default.
    How are you making the linked list?

    Quote Originally Posted by C_me_run
    However, in the interest of preventing un-defined behavior...does assigning NULL like that to those 3 pointers mean the next pointer inside of them is also NULL?
    It doesn't, because there is no next pointer inside of them. They are pointers to struct node objects, but not struct node objects themselves, thus they don't have any members, hence they don't have a next pointer 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

  2. #2
    Registered User
    Join Date
    Dec 2023
    Posts
    2
    Here are step-by-step ANSI C citations to confirm the explicit NULL assignments aren’t required.


    3.1.2,1 Scopes of Identifiers
    “If the declarator or type specifier that declares the identifier appears outside of any block or list of parameters, the identifier has file scope, which terminates at the end of the translation unit. If the declarator or type specifier that declares the identifier appears inside a block or within the list of parameter declarations in a function definition, the identifier has block scope, which terminates at the } that closes the associated block.”


    These struct declarations aren't within a block, so they have file scope.


    3.1.2.2 Linkages of Identifiers
    “If the declaration of an identifier for an object has file scope and no storage-class specifier, its linkage is external.”


    These file-scoped struct definitions have external linkage.


    3.1.2.4 Storage Durations of Objects
    “An object whose identifier is declared with external or internal linkage, or with the storage class specifier static has static storage duration.”


    These file-scoped, externally-linked struct definitions have static storage duration.


    3.5.7 Initialization -> Semantics
    “If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant.”


    These file-scoped, externally-linked struct definitions with non-explicit static storage duration are initialized with NULL.

  3. #3
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,127
    Quote Originally Posted by entrocraft View Post
    Here are step-by-step ANSI C citations to confirm the explicit NULL assignments aren’t required.
    ...
    What document are you quoting? Even in the C90 Standard RFC, the sections are:
    6.1.2.1 Scopes of identifiers
    6.1.2.2 Linkages of identifiers
    ...
    You should be quoting the latest C18 Standard,

  4. #4
    Registered User
    Join Date
    Mar 2023
    Posts
    33
    Quote Originally Posted by laserlight View Post
    That's initialisation, not assignment.
    It's initialization and assignment, unless "=" is not an assignment operator (and yes it is)

    If you don't specify any constant for initialisation, they are zero initialised, which for pointers means that they are initialised to be null pointers.


    No, actually this is not true at all. Let me prove it:

    Code:
    #include<stdio.h>
    int main()
    {
      int *value;
    
    
      if(!value)
      {
        printf("it's a NULL pointer!\n");
      }
      else
      {
        puts("it's not a NULL pointer\n");
      }
    
    
      return 0;
    }
    Here's the output:

    it's not a NULL pointer


    And that's the reason I posted this anyway: it seems that the pointer inside of the struct is NULL when I allocate memory for it.





    Last edited by C_me_run; 12-08-2023 at 08:49 PM.

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