Syntax questions

I've been reading the others' code to try and learn how to extract wav sample data. In one of the examples I came across, there was an area that confused me. The original author created a structures:
1
2
3
4
5
6
7
8
9
10
11
12
typedef struct 
{
	WAVEFORMATEX WaveFormat; 
	HMMIO        hMmio;       
	DWORD        cbDataChunk;
} *HXWAVE;

typedef struct
{
	USHORT left;
	USHORT right;
} XSAMPLE;

Then in the main function had this bit:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main(void)
{
	HXWAVE  hWave  = NULL;
	XSAMPLE sample = { 0 };

	hWave = xWaveOpen(TEXT("C:\\WINDOWS\\MEDIA\\TADA.WAV"));

	while (xWaveGetNextSample(hWave, &sample) > 0)
	{
		printf("%u,%u\n", sample.left, sample.right);
		
	}
	
	xWaveClose(hWave);
	
	return 0;
}

The part I'm confused about is this:
XSAMPLE sample = { 0 };

What does the { 0 } mean?

Thanks,
Chris
I think I figured it out. Correct me if I'm wrong.
XSAMPLE sample = { 0 };

This statement is used to initialize the structure members during the declaration of a structure object. This statement initializes the first member of the structure and is equivalent to the following statement:
sample.left = 0;

If it had been written as:
XSAMPLE sample = { 0, 0 };

It would be equivalent to:
1
2
sample.left = 0;
sample.right = 0;


Chris
actually, XSAMPLE sample = { 0 }; is equivalent to
1
2
sample.left = 0;
sample.right = 0;

because if you're using this syntax (it's called "aggregate initialization"), then the members of the struct/array that aren't listed explicitly are zero-initialized.
Does this only work when the members are initialized to zero? I ask because I wrote my own little code where instead of zero I put a 5, "CAR sports = {5};":

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
typedef struct{
	int doors;
	int wheels;
  
} CAR;

int main ()
{
	CAR sports = {5};
	cout<< sports.doors <<", " <<sports.wheels <<endl;
	sports.doors = 2;
	sports.wheels = 4;
	cout<< sports.doors <<", " <<sports.wheels <<endl;

  system("pause");
  return 0;
}


When I run this I get:

1
2
3
5, 0
2, 4
Press anny key to continue ...
It's not 'when' it's always. Anything not listed is set to zero.

CAR sports = {5} is the same as CAR sports = {5, 0}, that is, sports.doors = 5; sports.wheels = 0;
Does this only work when the members are initialized to zero?


To clarify, when you use this syntax, any members you do not specify are constructed to zero, regardless of whatever values you DO specify.

Continuing with your CAR example:

1
2
3
4
CAR sports = {5};
// effectively the same as:
sports.doors = 5;  // explicitly specified
sports.wheels = 0;  // not specified, so set to zero 


This is different from not specifying any members, in which case none of them are zero'd, and instead they are left uninitialized.
I'd like to append that C++ has a more funny syntax

XSAMPLE sample = {};

It is the same as the C initialization

XSAMPLE sample = { 0 };
So in other words this is a shortcut to zero out all the members of a structure. For instance, if there were 10 members of a structure you could zero them out by initializing a structure object like this, using my car example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
typedef struct{
	int doors;
	int wheels;
        int windows;
        int mirrors;
        int passengers;
  
} CAR;

int main ()
{
	CAR sports = {0};	
        
        return 0;
}


So the first member is set to zero and any unlisted members are automatically set to zero, simply because they weren't listed.

Chris

*edit*
Just read vlad from moscow's post, we must have posted at the same time.

I'm using c++ so,

CAR sports = {};

is even more of a shortcut.
Last edited on
Topic archived. No new replies allowed.