Yes,
intsize_t n = sizeof(tbl) / sizeof(tbl[0]) is the most typical way to do this.
Please note that using int for array sizes is not the best idea.
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about CollectivesStack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Explore Stack InternalYes,
intsize_t n = sizeof(tbl) / sizeof(tbl[0]) is the most typical way to do this.
Please note that using int for array sizes is not the best idea.
Yes,
int n = sizeof(tbl) / sizeof(tbl[0]) is the most typical way to do this.
Yes,
size_t n = sizeof(tbl) / sizeof(tbl[0]) is the most typical way to do this.
Please note that using int for array sizes is not the best idea.
Yes,
int n = sizeof(tbl) / sizeof(tbl[0]) is the most typical way to do this. An optimization would be to change it instead to:
int n = sizeof(tbl) / sizeof(char) At compile time, the sizeof(char) will be converted to a constant, saving you a de-referencing operation.
Also, take note that sizeof() will NOT work as expected if you pass the pointer tbl to a function, due to pointer decay.
References:
Yes,
int n = sizeof(tbl) / sizeof(tbl[0]) is the most typical way to do this. An optimization would be to change it instead to:
int n = sizeof(tbl) / sizeof(char) At compile time, the sizeof(char) will be converted to a constant, saving you a de-referencing operation.
Also, take note that sizeof() will NOT work as expected if you pass the pointer tbl to a function, due to pointer decay.
References:
Yes,
int n = sizeof(tbl) / sizeof(tbl[0]) is the most typical way to do this.
Yes,
int n = sizeof(tbl) / sizeof(tbl[0]) is the most typical way to do this. An optimization would be to change it instead to:
int n = sizeof(tbl) / sizeof(char) At compile time, the sizeof(char) will be converted to a constant, saving you a de-referencing operation.
Also, take note that sizeof() will NOT work as expected if you pass the pointer tbl to a function, due to pointer decay.
References:
Yes,
int n = sizeof(tbl) / sizeof(tbl[0]) is the most typical way to do this.
Yes,
int n = sizeof(tbl) / sizeof(tbl[0]) is the most typical way to do this. An optimization would be to change it instead to:
int n = sizeof(tbl) / sizeof(char) At compile time, the sizeof(char) will be converted to a constant, saving you a de-referencing operation.
Also, take note that sizeof() will NOT work as expected if you pass the pointer tbl to a function, due to pointer decay.
References: