Originally Posted by
legendbb
I know I'm violating good practice, and trying to get a good principle of using "pointer of pointer". That seems to me: never assign address of a uninitialized pointer to a pointer of pointer.
You can follow such a rule, but there are cases when there isn't much use in say, initialising a pointer to be a null pointer because you are immediately going to pass its address to a function that will provide the actual initial value. Hence, I personally do not follow such a rule, but rather I declare variables near first use such that I can provide them with a sensible initial value, either through initialisation or soon after declaration say through a function call.
If you do want to follow the rule to never assign address of a uninitialized pointer to a pointer of pointer even when you don't need to, then why limit this to pointers? You would be inconsistent. I would suggest a more general rule: never assign the address of anything unitialised to a pointer, e.g., if you want to call scanf to read an int, you will not write:
Code:
int x;
if (scanf("%d", &x) == 1)
but rather:
Code:
int x = 0;
if (scanf("%d", &x) == 1)
The initialision of x to 0 would be pointless here since you will not use x unless the scanf assignment to x succeeds, but it would be consistent and hence help you avoid cases where it is not pointless but you forgot. That is, either you always take care to do the right thing (e.g., pass the address of an initialised pointer when needed, or not when not needed), or if you want to be paranoid about not doing the wrong thing, then be paranoid everywhere or your inconsistency will come back to bite you.