Sublime Forum

Array of structs in c language

#1

struct time testTimes[5] = {{11,59,59}, {12,0,0}, {1,29,59}, {23,59,59}, {21,21,58}};

The preceding line of code specifies an array named “testTimes” containing five struct objects of the type time…

It’s program 9.6 in the Programming in C text book…

If i’m not mistaken, the Sublime editor I work on does not “colorize” the command properly…

since time is the struct type …in my color scheme…it should be white…but is instead the same green color given to the name of the array …testTimes…

this caused me to think I had formatted something improperly, given a compiler error i was receiving…but in the end my error was discovered elsewhere…and the program worked despite the strange color coding…I would love it if an experienced C language coder could confirm my suspicions

0 Likes

#2

What build of Sublime Text 3 are you using? Are you using the C syntax? I’m running build 3126 and time is not highlighted as a type, but is white like you suggest.

0 Likes

#3

Thanks for responding…

I am using build 3126…color scheme monokai…and yes I am using C syntax…

the issue is consistent…I’ve since come across the same behavior with the following…

const struct month months[12] = {
	{31, {'J', 'a', 'n'}}, {28, {'F', 'e', 'b'}},
	{31, {'M', 'a', 'r'}}, {30, {'A', 'p', 'r'}},
	{31, {'M', 'a', 'y'}}, {30, {'J', 'u', 'n'}},
	{31, {'J', 'u', 'l'}}, {31, {'A', 'u', 'g'}},
	{30, {'S', 'e', 'p'}}, {31, {'O', 'c', 't'}},
	{30, {'N', 'o', 'v'}}, {31, {'D', 'e', 'c'}} 
};

…again an array of structs…this time a const variable type …but the same behavior as far as the coloration of the struct type …which in this case is “month” and shows up green…same as the array name “months”…which is a little confusing until you realize that it’s (…I think…) a glitch in sublime…since in other cases …such as in …

const struct month function();

…here the name of the function …“function” is green …while the struct type …“month” is white…

It seems the extra word for the necessary syntax of creating an array of structs throws off the sublime formatting…or maybe my logic is faulty…

0 Likes

#4

I am wondering if you installed a third-party package that replaced the C syntax. Perhaps https://packagecontrol.io/packages/C99 or https://packagecontrol.io/packages/C11?

The other thing you can check is if you have an override for the C.sublime-syntax in (data)/Packages/C++/.

Your new example also highlights correctly for me in 3126.

0 Likes

#5

…I’m pretty sure my sublime was uploaded and paid for directly from sublime and has been getting updates all along…

…but …you may be missing my point …or again …i may be misunderstanding the code…but…now that i figured out how to upload an actual image of the thing in question…you see how “month” is white in the “funky” sample …while “month” is green in the in the array sample…

…the word month represents the same thing in both examples …it is the struct type…

…but in the case of the array it shows up green …like the array name…
…whereas in the case of the function definition…“month” is white …while the “funky” the function name is green…

…finally, notice your own example…whatever color scheme you are using…month and months are BOTH white…indistinguishable as far as their purpose in the statement…

probably too much ado at this point …but I do appreciate the discussion …

Thanks! …aw shucks …this thing won’t let me post 2 images …maybe a second message with the funky function…

0 Likes

#6

Funky function…proper formatting…

0 Likes

#7

What color scheme are you using?

Is there any code in your file other than what you are posting screenshots of? If so, that could affect the lexing of the code.

I fully understand your examples, and both are functioning as intended in build 3126, to my knowledge. I don’t have an answer (yet) as to why your install is showing month and months in green, but hopefully the information you provide from the questions above will help.

In case you aren’t familiar with syntax highlighting in Sublime Text, I’ll give a very high-level overview here.

In short, a .sublime-syntax definition is written for a language that splits the source code into tokens. Each token is given a scope name. Example including things like entity.name.function (for function names in function definitions), storage.type for well-known data types like int and char.

After the file has been tokenized, it is highlighted using a color scheme (.tmTheme file). The color scheme defines what colors are applied to what scopes.

Currently we don’t differentiate between user-defined structs, unions and enum names and variable names. This is largely due to the face that the C preprocessor and macros make it rather difficult to properly identify the exact usage of an identifier. So, in the case you presented, month and months should both be highlighted with the same color. We don’t really know if month or months are type names, variable names or macros. Rather than in some situations highlighting is property and in other getting is wrong, we err on the side of being consistent.

The reason funky is highlighted in green in your second screenshot (assuming you are using Monokai) is that we can fairly certainly determine that funky is a function name, and your color scheme is set to highlight the scope name for a function definition (entity.name.function) to green.

1 Like

#8

Hey…I thank you again for the time spent on this matter…I am coming to see that it is a little more complex than I originally understood…but hey…I learned a new term in the process …“Lexing”…who knew?..a week ago i didn’t know what a struct was…so again thank you …and I sincerely hope I wasn’t too much of a bother to you…

1 Like

#9

…just in case you are on a mission of discovery at this point…i give you one last image…from one single screenshot…

it appears that initializing the listEm array variable …makes some kind of difference in the lexing…

…for what it’s worth…

0 Likes