• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Explain this C syntax to me!

tatteredpotato

Diamond Member
Code:
static unsigned int ps3remote_bits[] = {
	[PS3R_BIT_ENTER] = 0x0b,
	[PS3R_BIT_PS] = 0x43,
	[PS3R_BIT_SQUARE] = 0x5f,
	[PS3R_BIT_CROSS] = 0x5e,
	[PS3R_BIT_CIRCLE] = 0x5d,
	[PS3R_BIT_TRIANGLE] = 0x5c,
	[PS3R_BIT_R1] = 0x5b,
	[PS3R_BIT_L1] = 0x5a,
	[PS3R_BIT_R2] = 0x59,
	[PS3R_BIT_L2] = 0x58,
	[PS3R_BIT_LEFT] = 0x57,
	[PS3R_BIT_DOWN] = 0x56,
	[PS3R_BIT_RIGHT] = 0x55,
	[PS3R_BIT_UP] = 0x54,
	[PS3R_BIT_START] = 0x53,
	[PS3R_BIT_R3] = 0x52,
	[PS3R_BIT_L3] = 0x51,
	[PS3R_BIT_SELECT] = 0x50,
};

Now PS3R_BIT_* have already been defined (actually they're elements of an enum). About the only thing I can think of is if you're specifying the index of the value that you're initializing as you go along. I've done a fair amount of C programming but haven't encountered this syntax before.
 
interesting. It is essentially assigning specific index values of the array. Not exactly common syntax.

That was my best guess as to what was happening, but I always find it interesting when I stumble upon new syntax tricks I never knew about before.
 
Hm I think it's been covered, but I'll add a link:
http://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html

What you're seeing is part of C99; it's called a "designated initializer." I've found them to be pretty useful actually. They're more expansive than the example you posted--in particular, you can now initialize [0..10] to a value, [11..14] to something else, [15..290] to another, etc.

Keep in mind that if you specify ANY array value, ALL unspecified array values will be initialized to 0.

Along similar lines, you might want to give "compound literals" a look too.
 
It's interesting to see what stuff was only just added into the official standard in C99. Most notably "//" comments and inline functions. I learned C++ first so I always assume these basic things were always part of C.
 
It's interesting to see what stuff was only just added into the official standard in C99. Most notably "//" comments and inline functions. I learned C++ first so I always assume these basic things were always part of C.

I'm in the same boat. I learned 'C' as defined by what a C++ compiler would recognize and without classes. I didn't start figuring out the actual differences until a lot later. Even now, its not always obvious to me as I'm staring at C-ish code whether it is legit C or not.
 
I'm in the same boat. I learned 'C' as defined by what a C++ compiler would recognize and without classes. I didn't start figuring out the actual differences until a lot later. Even now, its not always obvious to me as I'm staring at C-ish code whether it is legit C or not.

You could always slap in the -ansi flag and watch things fail... 😀
 
I can pretty much take care of failure cases on my own 😉. I'm a bloody expert.

🙂 It takes real talent to make things fail 🙂 (At least, that is what I tell myself after doing a statement like if (thing = bob) ... not on purpose)
 
🙂 It takes real talent to make things fail 🙂 (At least, that is what I tell myself after doing a statement like if (thing = bob) ... not on purpose)

Man I hate that.

I spent 20 minutes debugging some communication code only to realize I unplugged the cable :\
 
Man I hate that.

I spent 20 minutes debugging some communication code only to realize I unplugged the cable :\

oooh those types of errors are annoying 🙂. I had one where I was communicating with the server via ajax, The server was verifying that a session was still active, and if not, would send some garbage response like "Not logged in". Well, you don't see that response, ever, on the web side of things 🙂. Needless to say took me like an hour to realize that my 30 minute sleep time had elapsed. (you stay logged in so long as you do something within 30 minutes of the last action, IE browse to a new part of the site.)
 
Man I hate that.

I spent 20 minutes debugging some communication code only to realize I unplugged the cable :\

haha, always check the simple solution first. my laptop died, so i backed up the harddrive and reformatted to get ready to send back, then found out the power cord wasn't hooked up all the way to the wall. 😀
 
Back
Top