-
How to find the size of a predefined char[] variable that holds a string?
Should sizeof be enough?
I don’t want to use strlen() since a ‘\0’ character may occur in the string, and for optimization reasons.
E.g.:char str[] = "Hello \x45\x10\x00 World!"; -
What do you do when there’re initials in the identifier? E.g. GetURLParameters().
I thought of adding an underscore: GetURL_Parameters(). What is the convention for this situation? -
How can I easily copy values to the fields of a struct that is pointed by a variable/pointer?
For example, I would like to do something similar to:struct { int bar1; char* bar2; callback bar3; } *foo = NULL; foo = malloc(sizeof *foo); *foo = { 0, "Hello World!", myFunc }; // This is wrong -
Why programmers sometimes use an ampersand to assign an address of a function to a function pointer? E.g.:
void (*callback)(void); callback = &myFunc;(Sorry, but I can’t edit well my post. It looks different when I edit, and when it’s shown to you)
How to find the size of a predefined char[] variable that holds a string?
Share
char[]and not achar *,sizeof()will give you the array size (which isstrlen() + 1for 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.MixedCaseIdentifiersare 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:
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
initfunction that sets all the fields to their default values.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).