简体   繁体   中英

Best way to check if pointer is initialized

Is there a way to check if pointer was initialized? I have a struct that cointain array of pointers

typedef struct foo
{
    int* ptr1;
    int* ptr2;
    int* ptr2;
    ...
    int* ptr100;
}

and then my code is assigning adresses to those pointers (inside loop). But before adress is assigned i want to check if pointer is already containing adress. I know that i can initliazie every pointer with NULL and then check this with:

if(ptr1 == NULL)
    do something

But is there way to write something similiar but without initialization of ptr ?

An uninitialized variable can contain any value. Therefore, it is not possible to find out if a variable is uninitialized as it can look just like an already initialized variable. The only way to make sure that a variable is initialized is to explicitly write a value (for example, NULL ) to it as you already noted in your question.

I have a struct that contains array of pointers [...] is there way to write something similar but without initialization of ptr ?

No.

Related: https://stackoverflow.com/a/19626617/694576

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM