c - Why is {typedef int* PTR;const PTR p=#} not equivalent to "const int* p=&num" but to "int *const p=&num"? -
this thing partly touched upon in question on so, casually not main question. confusion still persists, putting in separate question.
why following 2 statements equivalent int* const p=&num , not const int* p=&num when latter seems more logical , intuitive? rigorous reasons behavior of typedef?
typedef int* ptr; const ptr p=# and finally, in question 1 member remarks bad practice use typedefed pointers. have seen being used in many books , websites , seems convenient thing do. makes code more understandable. final word on it? should 1 avoid typedefed pointers as possible?
edit: , correct syntax typedef statement if intend following:
const int* const p=# edit: inadvertently forgot ask important thing. correct syntax using typedef statement following then?
const int* p=# //instead of int* const p=&num got
generally,
const type var = ini; declares const variable var of type type. so
const ptr p=# declares const variable p of type ptr, initialised address of num.
a typedef not textual alias, can't replace typedefed name expansion see results in.
if want get
const int* const p=# with typedef, must typedef including const int, e.g.
typedef const int *ci_ptr; and can write
const ci_ptr p = # (but don't, it's ugly).
and for
const int *p = # you can write
ci_ptr p = # and finally,in question 1 member remarks bad practice use
typedefed pointers.but have seen being used in many books , websites , seems convenient thing do,that makes code more understandable.
whether makes code more understandable or less depends. 1 thing in experience bad thing typedef pointer types names hide fact dealing pointers.
typedef struct list_node { int value; struct list_node next; } *node; for example 1 unfortunately common abuse. when read type node, don't suspect it's pointer. @ least typedef node_ptr. then, why typedef pointer @ all, typedefing structure , using node* shorter , clearer.
so final word on it? should 1 avoid
typedefed pointers as possible?
there's no ultimate authority on it, it's decision. follow coding style in company/project if there one, use judgment if you're coding on own.
Comments
Post a Comment