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 OverflowIn 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;
};
NULL is a pointer value, wrapper->element is not a pointer, therefore you cannot assign it NULL
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;
}Better option is to pass the user count to the function, so you don't need to iterate over the max length. Otherwise yes, you would need to check for null.
I would do that like this:
if (users[i] && users[i]->id == id) { ... }
using users[i] instead of users[i] != NULL because NULL is equal to 0 and any non-zero value is like true
You also need to check users because arrays are like pointers and they can be NULL so any use of the [] operator will result in an error.
c - How to initialize a struct to null? - Stack Overflow
struct - Returning NULL if structure initialization failed in C? - Stack Overflow
Initializing struct members: Is it safe to initialize a struct containing both integers and integer pointers to 0?
c - Initialize/reset struct to zero/null - Stack Overflow
You can't. NULL is a pointer whose value is set to zero, but your mark and space properties are not pointer values. In your code as you have it, they will both be value types, allocated as part of your Pair struct.
Change the variables to Segment * instead of Segment, and you will be able to set them to NULL.
In order to intialise two sub-struct, which each contain a single int member to what gets closest to NULL, i.e. init all ints to "0" you can use this code:
#include <stdio.h>
typedef struct Segment {
int microseconds;
} Segment;
typedef struct Pair {
Segment mark;
Segment space;
} Pair;
int main(void)
{
Pair mark_and_space = { {0}, {0} };
return 0;
}
If you want to init them to NULL, assuming that you think of pointers, which is the only thing which can cleanly be intialised to NULL, then see the other answers, which basically say "if you want to init to pointer values, then init pointers, not ints".
if( mystruct == NULL )
mystruct is not a pointer, so you cannot compare it with NULL.
You have three options:
- Add a status field to
MyStructto indicate whether the struct has been initialized correctly. - Allocate the struct on the heap and return it by pointer.
- Pass the structure as a pointer argument and return a status code (thanks @Potatoswatter).
A structure is not a pointer. If you want to be able to return NULL, you're going to have to allocate the structure on the heap so you can return a pointer to it, and let the caller clean up afterwards.
That way, you can indicate failure, something like:
MyStruct *init_mystruct (void) {
MyStruct *mystruct = malloc (sizeof (*mystruct));
if (mystruct != NULL)
return NULL;
int is_ok = 1;
/* do something ... */
/* everything is OK */
if( is_ok )
return mystruct;
/* something went wrong */
free (mystruct);
return NULL;
}
int main (void) {
MyStruct *mystruct = init_mystruct();
if (mystruct == NULL) {
/* error handler */
return -1;
}
free (mystruct);
return 0;
}
hospital seed late wine pause instinctive like ludicrous attractive enjoy
This post was mass deleted and anonymized with Redact
Define a const static instance of the struct with the initial values and then simply assign this value to your variable whenever you want to reset it.
For example:
static const struct x EmptyStruct;
Here I am relying on static initialization to set my initial values, but you could use a struct initializer if you want different initial values.
Then, each time round the loop you can write:
myStructVariable = EmptyStruct;
The way to do such a thing when you have modern C (C99) is to use a compound literal.
a = (const struct x){ 0 };
This is somewhat similar to David's solution, only that you don't have to worry to declare an the empty structure or whether to declare it static. If you use the const as I did, the compiler is free to allocate the compound literal statically in read-only storage if appropriate.
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