- If it's a
char[]
and not achar *
,sizeof()
will give you the array size (which isstrlen() + 1
for nul-terminated strings). If you need to work extensively with strings (especially ones that may have the'\0'
character in them and/or may not be nul-terminated) you should consider using a string library. It will make your life much easier. - Whatever you want. I personally never use capital letters in identifiers, because I think
MixedCaseIdentifiers
are ugly. There is no convention for naming identifiers, only conventions that some people like. Use whatever looks best to you (and your team). You can do this:
struct s { int bar1; char* bar2; callback bar3; }; struct s *foo = NULL; static struct s default = { 0, "Hello, world!", myFunc }; foo = malloc(sizeof *foo); *foo = s;
My standards-fu is failing me, so I'm not sure why this works (or even if it does absolutely) but I believe it does. If it doesn't work, you just need to make an
init
function that sets all the fields to their default values.- Consistency. When assigning an address to a pointer, it's normally necessary, i.e.
int i = 5, *j = &i;
so even though it's not strictly necessary for function pointers, some people like to do it so that all pointer assignments look the same. It's just aesthetics.
However, in the future, most of these questions are unrelated to each other. You shouldn't group four unrelated questions into one giant monster question, but instead ask them separately. It'll make you take more time for each one, and each will come out more coherently so we'll be able to give you better answers (and you'll learn more).