What's the difference between VARNAME_T and VARNAME_P in c/c++
I'm newbie in programming especially c and c++.
I often saw VARNAME_P and VARNAME_T in c/c++. What are the difference between those two?
Thanks in adva开发者_C百科nce
By convention, _t
is a suffix that means "type", so any type might have this suffix. _p
usually means "pointer", so it indicates a type that is a pointer to a given type.
They're mostly just convention for names. The language itself defines a few types like size_t
, which is the type to be used for storing the sizes of things, or ptrdiff_t
the type for storing the differences between pointers.
The _p
suffix is most likely a pointer type like:
typedef int * INT_P;
so you could replace all your:
int *xyz = NULL;
lines with:
INT_P xyz = NULL;
I don't ever recall seeing the _p
suffix in the actual standard so that's just user convention, no different to mine, which is to use p
and t
as prefixes:
typedef struct {
char name[100];
int age;
} tPerson;
char *pBuff = maloc (sizeof (tPerson));
Are you referring to the type naming convention, where a type or pointer to a type has _t
or _p
appended to it respectively? If so, there are no differences. If you think about it, there can't be, considering that these conventions have whatever meaning you decide to attach to them. For example, I may decide that all my variables that deal with length will have l_
as a prefix.
There is no such thing like C/C++. These are two different language with probably differing naming conventions.
The suffix _t
in identifier names is meant to indicate that this identifier is a typedef
. Usually they alias some of the standard types, in a way that is suitable for the concrete platform your are dealing with.
These names are generally reserved: the C standards define some of them, and the POSIX standard requires all such names to be reserved for future use by extensions of that standard.
Thus it is a bad idea to declare such identifiers in your own code, especially for something that is not a typedef
.
精彩评论