LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-04-2011, 10:34 PM   #1
tz_uw
LQ Newbie
 
Registered: Sep 2011
Posts: 7

Rep: Reputation: Disabled
Initialize struct within a struct to NULL


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}
	};
}
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?

thank you very much
 
Old 09-04-2011, 11:14 PM   #2
tbrand
Member
 
Registered: Jul 2006
Location: Toronto, Canada
Distribution: gentoo
Posts: 33

Rep: Reputation: 17
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}}
	};
}
 
1 members found this post helpful.
Old 09-05-2011, 12:11 AM   #3
tz_uw
LQ Newbie
 
Registered: Sep 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
Thumbs up

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?

Code:
	twoint c;
	foo a =
	{
		3,
		c,
		c
	};
 
Old 09-05-2011, 05:42 AM   #4
tbrand
Member
 
Registered: Jul 2006
Location: Toronto, Canada
Distribution: gentoo
Posts: 33

Rep: Reputation: 17
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.

This is what I suspect you're trying to achieve:

Code:
#include <stdio.h>
#include <string.h>

typedef struct
{
   int a[2];
}twoint;
typedef struct
{
   int c;
   twoint a;
   twoint b;
}foo;

int main()
{
   twoint c;

   memset( &c, 0, sizeof(twoint));

   foo a =
   {
      3,
      c,
      c
   };

   printf( "%d\n", a.c );
   printf( "%d\n", a.a.a[0] );
   printf( "%d\n", a.a.a[1] );
   printf( "%d\n", a.b.a[0] );
   printf( "%d\n", a.b.a[1] );
}
 
1 members found this post helpful.
Old 09-05-2011, 06:11 AM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
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.
Code:
int main()
{
    foo a;
    memset(&a, 0, sizeof(foo));
    a.c = 3;

    ...
}
An alternative for very simple structures is something like this, but I would not recommend it for complex structures:
Code:
int main()
{
    foo a = { 3,
              {0, 0},
              {0, 0}
            };

    ...
}
P.S. In the context above, "null" means zero, not NULL which is defined as (void*)0.

Last edited by dwhitney67; 09-05-2011 at 06:13 AM.
 
Old 09-05-2011, 02:32 PM   #6
tz_uw
LQ Newbie
 
Registered: Sep 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
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}
 
Old 09-05-2011, 03:39 PM   #7
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by tz_uw View Post
- 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 View Post
- For simple structure, initialization can be done using braces. ex.{0, 0}
Correct.
 
Old 09-05-2011, 03:54 PM   #8
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Question about Kernel pid_namespace - struct pid and struct upid sreeharsha.t Linux - Kernel 4 03-19-2010 04:41 AM
Typecast struct sockaddr in struct sockaddr_in sudhansu Linux - Kernel 1 02-17-2009 10:33 AM
GCC compile problem:struct A have a member variable which is just a struct type name? leon.zcom Programming 3 04-18-2008 04:40 PM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration