My question is the following: does that make a difference in memory / speed to use a type int for key ?
It is likely -- but not certain -- that the size of struct trieNodeTag
would remain the same if you modified the type of its key
member from char
to int
, because it is likely that the compiler lays out that struct so that the offset of pointer next
from the beginning of the structure is a multiple of four bytes. If you want to know for sure, then apply the sizeof()
operator to each version of the struct, and compare the results. The outcome will depend to some extent on the C implementation you use.
Is a char treated directly as an int ?
In the evaluation of most expressions, an operand of type char
is promoted to int
before it is operated upon. This is cheap, and it might even be free, but no, a char
is not treated directly as an int
.
Overall, then, if the code is equally correct with the key typed as either int
or char
, then you should see little or no performance difference between the two.
I think you're posing the wrong question, however. It is likely that one of the types int
and char
is a more natural fit to the intended use of member key
. I'd guess that's char
, but whichever it is, that's the one to use. Strive to write code that makes sense and works correctly. Use appropriate algorithms for the task, but don't sweat fine performance details until and unless you measure your performance and find it lacking.