How to set a Struct as NULL in C?

Asked

Viewed 6,206 times

3

I’m looking to see if there’s any content on struct then wanted to initialize the variables as NULL, for if they were equal to NULL would know that they were not created.

snippets:

Struct:

struct  Fila
{

    int         capacidade;
    float       *dados;
    int         primeiro;
    int         ultimo;
    int         nItens;

};

initialization:

struct  Fila
PPFila          =   (Fila)NULL,  <--- Erro de incompatibilidade
AUTFila         =   (Fila)NULL;  <--- Erro de incompatibilidade

Creating:

if(PPFila   ==  (Fila)NULL)
{

    //
    //  Cria as filas que gerencias as o envio das threads
    //
    criarFila(&PPFila,  20);
}

Does anyone have any tips on how I can do this?

  • 1

    Lucas, please use the "c++" tag only for C++ problems. C is a different language.

  • @Pabloalmeida the problem also fits for C++, if he wants a solution for both C and C++, he can do this.

  • @Lucasfernandes you want me to stay at tag C++?

  • 1

    @bigown The question title asks for a solution in C. Also, C++ has different ways of dealing with things being done in the code. A C++ response would tend to educate the PA about these differences, and this can be a waste of time if the tag has been placed by ignorance.

  • 1

    @bigown can leave so, my interest is even in C

3 answers

3

stddef.h defines NULL as:

#define NULL ((void *)0)

So do a cast of NULL for Fila, which is neither a type in this code, or assign NULL for a primitive type variable is "wrong". "NULL is not a value, "is a type pointer to the address 0.

Use a pointer to your structure, so you can initialize it as null.

struct Fila * PPFila = NULL;

2


Unable to set a struct with NULL as declared.

Fila f = NUll;

error: invalid initializer.

If you want to assign NULL to a struct you must declare it as a pointer:

Fila * PPFila = NULL,
     * AUTFila = NULL;
  • Using gcc and Clang, when assigning a NULL to an integer, it only sends me a Warning, but creates an executable anyway (Warning: incompatible Pointer to integer Conversion initializing 'int' with an Expression of type 'void *' [-Wint-Conversion])

  • @Amadeus What is the command you are using to compile the program? I am using gcc and did not give this Warning, could post the code you are using to appear this Warning?

  • First, my gcc is version 6.1.1. Second, I used the same basics gcc -o x x.c, but you can try with the option -Wall. In the ideone not even generates Warning

  • The only difference is that my gcc is in version 4.9.2, could show the code you are using to generate this Warning?

  • sorry, had not seen the code. So as I said in the post to use NULL one must declare the variable as pointer. Pointers are declared with '*' before the name. In this code the variable is declared as integer, not supporting the use of NULL.

  • For more information about using pointers I recommend reading of this article

  • You don’t understand the placement. It’s wrong to put NULL for primary variables, example int. What I’m putting down is that the compiler doesn’t always see this as a mistake. In the case of ideone, as put in the comment above, did not even generate the Warning. So the expression "Cannot assign NULL to this integer" is not true. It is possible yes, only that it is not correct

  • Yes, you can assign it because the memory address in a 32-bit program is an integer. You can even use the following command: number = (int) &number; to store its address in the variable. I wouldn’t know a practical use for it. but it’s possible

  • The discussion is already getting long and soon will cut all comments, so I will just reaffirm what I already said: Your post is wrong. It is possible to assign NULL to a variable of type int, for example. But it’s not right. Unfortunately the compiler won’t always help you with this. So far, I’ve only seen one Warning being generated. Commenting on your last comment, memory addresses are usually of the type unsigned int to avoid problems with signal extension.

  • I understood the problem, I edited the answer. Thank you

Show 5 more comments

1

Well, for startup, what I like to do is use the memset to set all variables to ZERO.

Example:

struct  Fila PPFila, AUTFila;
memset(&PPFile, 0, sizeof(PPFila));
memset(&AUTFile, 0, sizeof(AUTFila));

To know if it is initialized or not, you can use some parameter of its structure that says this, or create a specific one to tell if it has already been initialized. In your case, it seems the camp nItens does exactly that role. So, for checking:

if(PPFila.nItens == 0)
{
    //
    //  Cria as filas que gerencias as o envio das threads
    //
    criarFila(&PPFila,  20);
}
  • Thanks, it helped a lot

  • What to do in case the queue is already created and has 0 items? Is that possible? You will need to have a strict control to it.

  • To get an idea, go to '*Ppfila.dados' in if, this ends up generating an error, and locking the program.

  • looking at that point, it seems to make perfect sense

  • and what will happen if you try to create it again if it is already created but with 0 elements?

  • @Why Pilati said in the answer: "or create a specific one to say if it has already been initialized". If in the structure there is no variable that can be used for this role, create one, maybe something like int inicializada; and then check if it is zero, if it is, initialize and put 1. I just made a suggestion in his case, which now it seems to me that it is better to use the variable capacidade

  • @Amadeus Capacity really seems to be the best field created for this purpose, it makes no sense for a queue to have a capacity 0.

  • @Lucasfernandes So I noticed the structure has a pointer to float, so if the queue has already been created and is creating again the reference to the old pointer will be lost, thus causing the famous memory leaks

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.