Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. why i am getting error "unknown type name ‘PTR’ " ?
QtWS25 Last Chance

why i am getting error "unknown type name ‘PTR’ " ?

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 3 Posters 1.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on 28 Mar 2022, 07:35 last edited by
    #1

    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 ?

    J J 2 Replies Last reply 28 Mar 2022, 07:43
    0
    • Q Qt embedded developer
      28 Mar 2022, 07:35

      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 ?

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 28 Mar 2022, 07:43 last edited by
      #2

      @Qt-embedded-developer said in why i am getting error "unknown type name ‘PTR’ " ?:

      #define char* PTR

      #define PTR char*


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      4
      • Q Qt embedded developer
        28 Mar 2022, 07:35

        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 ?

        J Offline
        J Offline
        JonB
        wrote on 28 Mar 2022, 08:07 last edited by JonB
        #3

        @Qt-embedded-developer
        Are you aware that your code illustrates an important rule about C/C++ type declaration statements, and that your ptr a,b is very different from PTR A,B?

        typedef char* ptr;
        ptr a,b;
        

        declares both a & b to be of type char*.

        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 type char* but B is of type char --- no *. That's how C/C++ declarations work.

        One moral: in modern C++ keep away from #defines for types, prefer typedef.

        1 Reply Last reply
        5

        1/3

        28 Mar 2022, 07:35

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved