why i am getting error "unknown type name ‘PTR’ " ?
Solved
C++ Gurus
-
when i am running below program code i am getting error :unknown type name ‘PTR’
#include <iostream> typedef char* ptr; #define char* PTR int main() { ptr a,b; PTR A,B; cout<< sizeof(a); return 0; }
I want to know how to resolve it ?
-
@Qt-embedded-developer said in why i am getting error "unknown type name ‘PTR’ " ?:
#define char* PTR
#define PTR char*
-
@Qt-embedded-developer
Are you aware that your code illustrates an important rule about C/C++ type declaration statements, and that yourptr a,b
is very different fromPTR A,B
?typedef char* ptr; ptr a,b;
declares both
a
&b
to be of typechar*
.However
#define PTR char* PTR A,B; // expands to: char* A,B; // but read as: char *A, B;
In C/C++ this might look like it's equivalent to previous, but it is not.
A
is indeed of typechar*
butB
is of typechar
--- no*
. That's how C/C++ declarations work.One moral: in modern C++ keep away from
#define
s for types, prefertypedef
.