You already did.
char buf[100];
If you reference *buf within this scope, the compiler knows that *buf is a char, and it also knows that sizeof buf is 100.
Type: Posts; User: c99tutorial
You already did.
char buf[100];
If you reference *buf within this scope, the compiler knows that *buf is a char, and it also knows that sizeof buf is 100.
In C, aggregates are treated as pointers. So buf is a pointer to char, *buf is a char, and sizeof(char) is 1, of course. If buf is an array of something else, then the size will change accordingly.
...
Suppose you write this
char buf[1000];
printf("%d\n", NELEMS(buf));
sizeof buf is 1000 and sizeof *buf is 1, so the result is 1000 elements. The nice thing about the macro is that it...
I usually use this macro:
#define NELEMS(obj) (sizeof(obj)/sizeof(*obj))
Then you can just write NELEMS(array)