ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
#include <stdio.h>
typedef struct
{
int a[2];
}twoint;
typedef struct
{
int c;
twoint a;
twoint b;
}foo;
int main()
{
foo a =
{
3,
{NULL},
{NULL}
};
}
test.c: In function ‘main’:
test.c:17: warning: missing braces around initializer
test.c:17: warning: (near initialization for ‘a.a.a’)
test.c:17: warning: initialization makes integer from pointer without a cast
test.c:18: warning: initialization makes integer from pointer without a cast
test.c:14: warning: unused variable ‘a’
test.c:20: warning: control reaches end of non-void function
What's the correct way to initialize struct within a struct to NULL?
For the reason you're getting the missing braces warnings see my answer to your earlier thread.
The reason you're getting the cast warnings is because you're trying to initialize an array of two integers to NULL value. NULL is the value of an address that is guaranteed not to exist not of an integer. So, in the context of your program something like this would make sense:
Code:
#include <stdio.h>
typedef struct
{
int *a[2];
}twoint;
typedef struct
{
int c;
twoint a;
twoint b;
}foo;
int main()
{
foo a =
{
3,
{{NULL, NULL}},
{{NULL, NULL}}
};
}
Hi Tbrand, thank you so much for your response!
In case a more complex struct is used here instead of "twoint".
Does it make sense to do something like this? and will c be NULL by default?
C is a very low level language and the compiler does not guarantee any specific initial values and does not perform any complex data conversions implicitly. C is closer to assembler than most other languages you may use. Furthermore, NULL in C is of type pointer and is not defined for other data types.
Your last piece of code will compile but will not have the effect I believe you expect because twoint c itself is not initialized.
Using memset() is probably the best/fastest way to initialize a structure to a "null". If a particular structure member needs to be initialized to a different value other than "null", then set it manually.
Thank you all very much for helping me out. I have found exactly what I was looking for here.
In summary:
- NULL is a pointer in C. Therefore, to initialize data type other than pointer memset( &c, 0, sizeof(twoint)); is the best way.
- For simple structure, initialization can be done using braces. ex.{0, 0}
- NULL is a pointer in C. Therefore, to initialize data type other than pointer memset( &c, 0, sizeof(twoint)); is the best way.
NULL being a pointer has nothing to do with using memset().
Also, NULL is the same as 0 (it's not specifically a pointer), but it's best to use "0" for integers and "NULL" for pointers to make your code easier to understand.
Quote:
Originally Posted by tz_uw
- For simple structure, initialization can be done using braces. ex.{0, 0}
I favor using memset() to initialize a static structure, especially a large one, because otherwise you're going to be burning-up bytes in the object file for "just a bunch o' zeroes." Or, you can use calloc().
If you find yourself doing a lot of this kind of stuff, seriously consider using C++ in a fashion which can be more-or-less upward compatible with C. The rationale is quite simple: to the extent that the language implementers might have done hard-work for you, let them be the ones to have done it.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.