How do you check if a struct is initialized in C?

User

I declared a struct and I want to check if it has been initialized yet. How do I do this?

I've tried:

struct mystruct str1;

if(str1 != NULL)

if(str1->name != NULL)

The struct is a linked list, and also contains properties of type int and char among others.

clearlight

If you declare the struct outside of any function including main(), the struct and its contents will be initialized to zero. As pointed out in the comments, that means different things for different data types.

  1. integers (e.g. char, short, int, long, unsigned, unsigned int, long long, etc..) will be 0
  2. float and double will be numeric 0.0 [but the bytes they are comprised of may not be]
  3. pointers will be NULL
  4. struct padding may be undefined.
  5. arrays will be zeroed as appropriate for their type.

If you define the struct or any variables non-static within a function, they are undefined. On some implementations they happen to contain whatever is on the stack in many implementations, but that is architecture-dependent. In any case, you can assume structs, arrays and variables that are declared in a function without the static word in front of them are uninitialized and contain garbage.

You normally would not test to see if a struct is initialized, you just have to know the circumstances you defined the in, and whether you've initialized it (and its subelements) or not.

The only way you could determine if a struct was initialized would be to check each element within it to see if it matched what you considered an initialized value for that element should be. If you want to check to see if a pointer to a struct is initialized to a pre-defined state, you'd just see if it contains either NULL or a specific address you initialized it to.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do you edit a repeatedly initialized label?

From Dev

How do you perform selection sort on a struct in C?

From Dev

How to check if a variable has been initialized in C?

From Dev

How do I check if an Object has been initialized/created in C++?

From Dev

How do you check if a specific operator is defined for a template in C++?

From Dev

How do you "declare" a global struct?

From Dev

how do you use a UIbutton In a struct for segue

From Dev

How do you initialize a union member after the union itself was initialized?

From Dev

How to check if int is initialized?

From Dev

How to check that jcrop is initialized

From Dev

How to check if a struct is NULL in C or C++

From Dev

How do you convert a C data structure to a C# struct that will allow you to read the data type from a file?

From Dev

How do you check for a scalar in R?

From Dev

How do you check an array for a range in Ruby?

From Dev

How do you check if a tag is present on a page?

From Java

How do you check if a variable is an array in JavaScript?

From Java

How do you check "if not null" with Eloquent?

From Dev

How do you verify/check kivy version?

From Dev

How do you check for the type of variable in Elixir

From Dev

How do you check if & variable is set in directive

From Dev

How do you check the elements of a submatrix in Prolog

From Dev

How do you check if a string is not equal to an object?

From Dev

How do you check if a property is undefined in qml?

From Dev

How do you conditionally check for an escape character?

From Dev

How do you check if a string is a palindrome in java?

From Dev

How do you check systemd update content?

From Dev

How do you check if input is numerical in Python?

From Dev

How do you check if userinput was something else?

From Dev

How do you check whether you're in a transaction with ZODB in Python?

Related Related

  1. 1

    How do you edit a repeatedly initialized label?

  2. 2

    How do you perform selection sort on a struct in C?

  3. 3

    How to check if a variable has been initialized in C?

  4. 4

    How do I check if an Object has been initialized/created in C++?

  5. 5

    How do you check if a specific operator is defined for a template in C++?

  6. 6

    How do you "declare" a global struct?

  7. 7

    how do you use a UIbutton In a struct for segue

  8. 8

    How do you initialize a union member after the union itself was initialized?

  9. 9

    How to check if int is initialized?

  10. 10

    How to check that jcrop is initialized

  11. 11

    How to check if a struct is NULL in C or C++

  12. 12

    How do you convert a C data structure to a C# struct that will allow you to read the data type from a file?

  13. 13

    How do you check for a scalar in R?

  14. 14

    How do you check an array for a range in Ruby?

  15. 15

    How do you check if a tag is present on a page?

  16. 16

    How do you check if a variable is an array in JavaScript?

  17. 17

    How do you check "if not null" with Eloquent?

  18. 18

    How do you verify/check kivy version?

  19. 19

    How do you check for the type of variable in Elixir

  20. 20

    How do you check if & variable is set in directive

  21. 21

    How do you check the elements of a submatrix in Prolog

  22. 22

    How do you check if a string is not equal to an object?

  23. 23

    How do you check if a property is undefined in qml?

  24. 24

    How do you conditionally check for an escape character?

  25. 25

    How do you check if a string is a palindrome in java?

  26. 26

    How do you check systemd update content?

  27. 27

    How do you check if input is numerical in Python?

  28. 28

    How do you check if userinput was something else?

  29. 29

    How do you check whether you're in a transaction with ZODB in Python?

HotTag

Archive