Small and efficient hash map like behavior in a small array. Social links: Website: cacharle.xyz Github: github.com/cacharle Linkedin: / charles-cabergs-328aa8214
The basic usecase for designated initialisers is when you want to initialise a very large but sparse array. That was already something very useful to me. This is just icing on the cake.
I love this series, really great! 2:45 In fact, the subscript operator [ ] requires the type int (not unsigned int - although this is automatically converted). A negative number is allowed as long as it is ensured that the memory access behind the index 0 is allowed. x = ptr[-3]; // O. K. A character constant, e.g. 'A', is also of type int in C - and not of type char, as is often wrongly assumed. This is different from C++, where a character constant is of type char. Which would also fit your examples: You can also apply this idea to an array of char pointers enum { DENNIS = 0, BRIAN, KEN, CHARLES }; char const * const names[] = { "Dennis Ritchie", "Brian Kernighan", "Ken Thompson", "Charles Cabergs" }; ... printf(" %s", names[CHARLES]); Cheers, starcow
Thank you for the thorough comment 😄 The fact that negative numbers are allowed seems quite unintuitive but I'm guessing legacy is to blame. As for the character literal, I'm very surprised they are 4 bytes.. I thought you had to prefix them with L'A' for them to be 4 bytes long but that is indeed only in C++. Definitely would have been a pitfall I was going to run into at some point 😅
That syntax is pretty new to me, so are we defining indexes to arrays as numerical ascii values, and then stored in the array are structs, which contain a function pointer and an int? How does all that work? It looks really cool, do you have a link to a github repo I could look at?
yes, it's just a syntax to have the index in any order you want, I think the index can be anything that can be converted to an unsigned int (ascii, int, enum are all fine) I guess you're referring to my sed project's parsing. Here is the lookup table with the struct and function pointers: github.com/cacharle/sed/blob/590c0290b0b54554b3fad663c23c3f9f630f8e9a/src/parse.c#L348C3-L348C3 Here is the lookup table for escape characters: github.com/cacharle/sed/blob/590c0290b0b54554b3fad663c23c3f9f630f8e9a/src/parse.c#L67
They are called Designated Initializers, I knew about them. But this is the first time I've seen an actual use case. Thank you.
Thank you, I didn't know the name for those.
The basic usecase for designated initialisers is when you want to initialise a very large but sparse array. That was already something very useful to me. This is just icing on the cake.
I love this series, really great!
2:45
In fact, the subscript operator [ ] requires the type int (not unsigned int - although this is automatically converted).
A negative number is allowed as long as it is ensured that the memory access behind the index 0 is allowed.
x = ptr[-3]; // O. K.
A character constant, e.g. 'A', is also of type int in C - and not of type char, as is often wrongly assumed. This is different from C++, where a character constant is of type char.
Which would also fit your examples:
You can also apply this idea to an array of char pointers
enum
{
DENNIS = 0,
BRIAN,
KEN,
CHARLES
};
char const * const names[] =
{
"Dennis Ritchie",
"Brian Kernighan",
"Ken Thompson",
"Charles Cabergs"
};
...
printf("
%s", names[CHARLES]);
Cheers, starcow
Thank you for the thorough comment 😄
The fact that negative numbers are allowed seems quite unintuitive but I'm guessing legacy is to blame.
As for the character literal, I'm very surprised they are 4 bytes.. I thought you had to prefix them with L'A' for them to be 4 bytes long but that is indeed only in C++.
Definitely would have been a pitfall I was going to run into at some point 😅
I thought it was a video about c# because of the title :D
Did I manage to convert you to the dark side though?
Currently porting a project to C, I used this feature to convert some enums for which I need a string value to map to.
That syntax is pretty new to me, so are we defining indexes to arrays as numerical ascii values, and then stored in the array are structs, which contain a function pointer and an int? How does all that work? It looks really cool, do you have a link to a github repo I could look at?
yes, it's just a syntax to have the index in any order you want, I think the index can be anything that can be converted to an unsigned int (ascii, int, enum are all fine)
I guess you're referring to my sed project's parsing.
Here is the lookup table with the struct and function pointers: github.com/cacharle/sed/blob/590c0290b0b54554b3fad663c23c3f9f630f8e9a/src/parse.c#L348C3-L348C3
Here is the lookup table for escape characters: github.com/cacharle/sed/blob/590c0290b0b54554b3fad663c23c3f9f630f8e9a/src/parse.c#L67
Thank you! I don't think those links work though :( @@cacharle
@@cooltrashgamer ah yes, forgot to make the repository public 😅, they should work now
Awesome, thank you so much 🙏 @@cacharle
Awesome!
Thank you :)