In C NULL is generally defined as the following

#define NULL ((void*)0)

This means that it's a pointer value. In this case your attempting to assign a pointer (NULL) to a non-pointer value item::element and getting the appropriate message. It seems like your intent is to have element be a pointer here so try the following

struct item {
  struct elem* element;
};
Answer from JaredPar on Stack Overflow
🌐
Reddit
reddit.com › r/c_programming › i need help with checking for null in array of structure pointers
r/C_Programming on Reddit: I need help with checking for NULL in array of structure pointers
February 19, 2022 -

Hi! I started learning C a few months ago and I'm working on a simple application, it doesn't really matter.

What matters is that I'm not sure if I'm doing it alright. The application has an array of pointers to user structures, and I want this function to return a pointer to a structure that has the correct id.

I just wonder if it is required to check if the pointer isn't NULL for every pointer in the array, or is there a better way to do it?

And I know that it would be easier to read with typedefs (probably) but it's easier for me to understand what exactly I have to pass to the function :>

I also made sure for the function to return NULL if there is no user with the given id.
I tested this code and it works, I just don't know if there's a better way to do it, if it's readable or if my way of thinking is wrong/correct.

I just want to improve :3

enum UserRole {
    REGULAR,
    ADMIN,
    MASTER
};

struct User {
    enum UserRole role;
    uint64_t id;
    char* name;
    char* surname;
};

struct User *get_user_by_id(struct User* users[MAX_USERS_AMOUNT], uint64_t id)
{
    for (int i = 0; i < MAX_USERS_AMOUNT; i++) {
        if (users[i] != NULL && users[i]->id == id) {
            return users[i];
        }
    }

    return NULL;
}
Discussions

c - How to initialize a struct to null? - Stack Overflow
Since Pair fields are not pointers NULL is not a good choice to initialize a structure. More on stackoverflow.com
🌐 stackoverflow.com
struct - Returning NULL if structure initialization failed in C? - Stack Overflow
It has a structure and a function to initialize that structure. What I'm trying to do is to return NULL if there was a failure in that function. ... code.c: In function ‘main’: code.c:13: error: invalid operands to binary == (have ‘MyStruct’ and ‘void *’) code.c: In function ... More on stackoverflow.com
🌐 stackoverflow.com
Initializing struct members: Is it safe to initialize a struct containing both integers and integer pointers to 0?
Zero is pretty special. It sets characters to '\0' (ASCII NUL), and if they are char arrays that makes a string zero-length too. All integers become zero (short, int, long). Float and double (and long double) become 0.00. That is defined in IEEE 754. Pointers become NULL. Technically a system can have a NULL pointer value being something other that zero (C language does not strictly define it that way) but in 40 years I never saw a system that used anything other that zero. TL;DR Yes -- setting everything to zero works. More on reddit.com
🌐 r/C_Programming
13
11
March 14, 2022
c - Initialize/reset struct to zero/null - Stack Overflow
Note also the comment about where ... assign null values. My previous comment just points out the reality that 0 bits invariably does correspond to floating point zeros. 2011-07-31T20:19:33.837Z+00:00 ... The way to do such a thing when you have modern C (C99) is to use a compound literal. ... This is somewhat similar to David's solution, only that you don't have to worry to declare an the empty structure or whether ... More on stackoverflow.com
🌐 stackoverflow.com
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 151817-assign-null-value-struct-check-if-null.html
assign null value to a struct and check if it is null
October 30, 2012 - 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.
🌐
Unstop
unstop.com › home › blog › null pointer in c | a detailed explanation with examples
Null Pointer In C | A Detailed Explanation With Examples
May 3, 2024 - Termination Indication: In data structures like linked lists or binary trees, NULL pointers are used to signify the end of a structure or indicate that a pointer does not point to any further elements.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › null-pointer-in-c
NULL Pointer in C - GeeksforGeeks
January 10, 2025 - The Null Pointer is the pointer that does not point to any location but NULL. According to C11 standard: “An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.
🌐
Ex-parrot
ex-parrot.com › ~chris › random › initialise.html
How to initialise structures to all-elements-zero-or-null
You don't have to initialise every element of a structure, but can initialise only the first one; you don't need nested {} even to initialise aggregate members of a structure. Anything in C can be initialised with = 0; this initialises numeric elements to zero and pointers null.
Find elsewhere
🌐
Quora
quora.com › In-the-C-language-is-it-true-that-a-struct-itself-as-opposed-to-the-individual-items-comprising-the-struct-cannot-be-assigned-to-NULL-If-so-why
In the C language, is it true that a struct itself - as opposed to the individual items comprising the struct - cannot be assigned to NULL? If so, why? - Quora
Answer (1 of 7): A struct is a composite type, consisting of one actual size in bytes, that can be broken down into individual typed components. As such, it can be assigned in whole only by either giving it a list of initial values at its first definition, or assigning it to another struct of th...
Top answer
1 of 3
15
Zero is pretty special. It sets characters to '\0' (ASCII NUL), and if they are char arrays that makes a string zero-length too. All integers become zero (short, int, long). Float and double (and long double) become 0.00. That is defined in IEEE 754. Pointers become NULL. Technically a system can have a NULL pointer value being something other that zero (C language does not strictly define it that way) but in 40 years I never saw a system that used anything other that zero. TL;DR Yes -- setting everything to zero works.
2 of 3
6
u/Paul_Pedant gives a decent short explanation, but (as is almost always the case with C) there are some wrinkles worth understanding. To that end, I recommend reading this excellent blog post by Noah Pendleton: C Structure Padding Initialization , though I'll give the short version here. I'd add two things to u/Paul_Pedant 's explanation. First, regarding pointers, they're correct that initializing a pointer to 0 actually makes it a NULL pointer. The bit pattern of the pointer's value might not end up being all zeros, though. A literal 0 in C is special that way. Second, while initializing a struct with {0} is safe and correct, it does leave any padding unset. This may result in a security issue, since any previous data in those bytes may be unaltered (it's actually unspecified -- some compilers will zero the padding anyway, though not necessarily for all target platforms), and thus potential allow for data leak. One can use to zero out the struct (I've added a char element to your struct to demonstrate the padding issue): struct mystruct { char c; int my_int; int *my_int_pointer; }; struct mystruct foo; memset(&foo, 0, sizeof struct mystruct); This will safely zero out the padding between c and my_int. However, it also zeros out my_int_pointer, which may not be correct, since a given implementation may not actually use a pattern of all zeros for the NULL pointer (as u/Paul_Pedant pedantically proclaimed previously ... sorry. not sorry. :-) . This is, as they said, vanishingly rare today. If you're using a platform (e.g. some DSPs) that does use another value for NULL, the memset will not initialize things correctly. I tend to use {0} init in most cases, unless security is a concern. In those cases, I either use a secure init function (if the environment provides one), or memset followed by explicitly initializing the pointer members, if any: struct mystruct { char c; int my_int; int *my_int_pointer; }; struct mystruct foo; memset(&foo, 0, sizeof struct mystruct); foo.my_int_pointer = NULL; Again, rarely necessary, but worth understanding the situation.
🌐
Cplusplus
cplusplus.com › forum › beginner › 210381
Structure Pointer Become Null at every c - C++ Forum
Structure Pointer Become Null at every c · bird1234 (126) Write your question here. Peter87 (11251) The functions receives copies of the pointers that you pass to them so any modifications to the pointers inside the function will not affect the original pointers in main.
🌐
Dot Net Tutorials
dotnettutorials.net › home › null pointer in c
Null Pointer in C Language with Examples - Dot Net Tutorials
November 16, 2023 - In C programming, a null pointer is a pointer that does not point to any valid memory location. It’s a special type of pointer used to indicate that it is not intended to point to an accessible memory location.
🌐
Swarthmore College
cs.swarthmore.edu › ~newhall › cs31 › resources › C-structs_pointers.php
CS31: Intro to C Structs and Pointers
ptr = NULL; ------ ------ cptr = NULL; ptr | NULL |-----| cptr | NULL |----| ------ ------ ... Use *var_name to dereference the pointer to access the value in the location that it points to.
🌐
IncludeHelp
includehelp.com › c-programs › an-example-of-null-pointer-in-c.aspx
An Example of Null pointer in C
March 10, 2024 - By IncludeHelp Last updated : March 10, 2024 · The word "NULL" is a constant in C language and its value is 0. In case with the pointers - if any pointer does not contain a valid memory address or any pointer is uninitialized, known as "NULL pointer".
🌐
LinuxQuestions.org
linuxquestions.org › questions › programming-9 › initialize-struct-within-a-struct-to-null-901142
Initialize struct within a struct to NULL
September 4, 2011 - Code: #include typedef struct { int a[2]; }twoint; typedef struct { int c; twoint a; twoint b; }foo; int main() { foo a = { 3, {NULL},
🌐
Reddit
reddit.com › r/c_programming › [need explanation] casting null pointer to get sizeof() struct members
r/C_Programming on Reddit: [Need explanation] casting null pointer to get sizeof() struct members
March 19, 2025 -

In this Stackoverflow post[1] is stumbled upon a 'trick' to get the size of struct members like so: sizeof(((struct*)0)->member) which I struggle to comprehend what's happening here.

what I understand:
- sizeof calculates the size, as normal
- ->member dereferences as usual

what I don't understand:
- (struct*) 0 is a typecast (?) of a nullptr (?) to address 0 (?)

Can someone dissect this syntax and explain in detail what happens under the hood?

[1] https://stackoverflow.com/a/3553321/18918472

Top answer
1 of 5
31
It helps to go back to first principals and build it up. Given a struct: struct foo { int a; }; The problem is sizeof() ONLY works on FULL types or instances not types of member fields. We CAN'T do this to get at a struct's member field ... printf( "sizeof foo : %zu\n", sizeof( struct foo ) ); // OK // printf( "sizeof foo.a: %zu\n", sizeof( struct foo.a ) ); // ERROR ... so we need to use a temporary. struct foo f; printf( "sizeof f.a : %zu\n", sizeof( f.a ) ); // OK We could use a pointer instead, and deference that pointer: struct foo *p = &f; printf( "sizeof p->a : %zu\n", sizeof( p->a ) ); // OK However, the address of the temporary doesn't matter! We could have a pointer where the instance of foo is at address 0! struct foo *q = 0; printf( "sizeof q->a : %zu\n", sizeof( q->a ) ); // OK We can remove the temporary entirely by inlining it -- pretending we have the struct at address 0. printf( "sizeof 0->a : %zu\n", sizeof( ((struct foo*)0)->a) ); // OK We can turn that into a macro to help readability: #define MEMBER_SIZE( type, member ) (sizeof( ((struct type *)0)->member )) printf( "macro foo,a : %zu\n", MEMBER_SIZE(foo,a) ); // OK If you already know the struct type you can avoid passing the type. #define FOO_MEMBER_SIZE(member) (sizeof( ((struct foo *)0)->member )) printf( "macro a : %zu\n", FOO_MEMBER_SIZE(a) ); // OK Demo below: #include struct foo { int a; }; int main() { printf( "sizeof foo : %zu\n", sizeof( struct foo ) ); // OK // printf( "sizeof foo.a: %zu\n", sizeof( struct foo.a ) ); // ERROR struct foo f; printf( "sizeof f.a : %zu\n", sizeof( f.a ) ); // OK struct foo *p = &f; printf( "sizeof p->a : %zu\n", sizeof( p->a ) ); // OK struct foo *q = 0; printf( "sizeof q->a : %zu\n", sizeof( q->a ) ); // OK printf( "sizeof 0->a : %zu\n", sizeof( ((struct foo*)0)->a) ); // OK #define MEMBER_SIZE( type, member ) sizeof( ((struct type*)0)->member ) printf( "macro foo,a : %zu\n", MEMBER_SIZE(foo,a) ); // OK #define FOO_MEMBER_SIZE(member) (sizeof( ((struct foo *)0)->member )) printf( "macro a : %zu\n", FOO_MEMBER_SIZE(a) ); // OK return 0; }
2 of 5
10
sizeof doesn't evaluate the operand unless it's a variable length array. So, the cast and -> are fine as they're not evaluated. After all, the sizeof operator only needs to know the type of the operand to figure out the size. So, there's no point at all in evaluating the value of the operand expression (unless it's a variable length array).
🌐
devRant
devrant.com › rants › 2202420 › whats-the-best-way-to-null-terminate-a-struct-array
struct array - What's the best way to null-terminate a struct array? - devRant
If you must have a null-terminated array: struct X xarr[XARRLEN + 1]; memset(&xarr[XARRLEN], 0, sizeof(struct X)); If you are using less elements (say i), repeat memset() for the (i + 1)'th element.