Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. strange char* compile error?

strange char* compile error?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
11 Posts 6 Posters 0 Views 1 Watching
  • 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.
  • G Offline
    G Offline
    George_George
    wrote on last edited by
    #1

    Hello everyone, Anything wrong with my code below? I got the compile error!

    typedef unsigned char BYTE;

    void foo(char* & p)
    {
    return;
    }

    int main()
    {
    BYTE * buffer;
    // error C2664: 'foo' : cannot convert parameter 1 from 'char *' to 'char *&'
    foo ((char*)buffer);

    return 0;
    

    }

    thanks in advance, George

    A _ S 3 Replies Last reply
    0
    • G George_George

      Hello everyone, Anything wrong with my code below? I got the compile error!

      typedef unsigned char BYTE;

      void foo(char* & p)
      {
      return;
      }

      int main()
      {
      BYTE * buffer;
      // error C2664: 'foo' : cannot convert parameter 1 from 'char *' to 'char *&'
      foo ((char*)buffer);

      return 0;
      

      }

      thanks in advance, George

      A Offline
      A Offline
      Alexander M
      wrote on last edited by
      #2

      this isnt strange as foo required char*& and not char*.

      Don't try it, just do it! ;-)

      G 1 Reply Last reply
      0
      • A Alexander M

        this isnt strange as foo required char*& and not char*.

        Don't try it, just do it! ;-)

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #3

        My confusion is reference type is the same as original type, for example when type Foo is needed, we could pass Foo or pass Foo&. So, I think then char* & is needed I could pass char*? What is wrong with my understanding? :-)

        R 1 Reply Last reply
        0
        • G George_George

          My confusion is reference type is the same as original type, for example when type Foo is needed, we could pass Foo or pass Foo&. So, I think then char* & is needed I could pass char*? What is wrong with my understanding? :-)

          R Offline
          R Offline
          Rolf Kristensen
          wrote on last edited by
          #4

          BYTE is an unsigned char (8 bit) char* is a pointer (32 bit) When converting BYTE to char* then it needs to create a temporary variable. It is not possible to make return-by-reference with temporary variables. Instead make the conversion from BYTE to char* by using a local variable, and then pass that local variable to the function.

          G G 2 Replies Last reply
          0
          • G George_George

            Hello everyone, Anything wrong with my code below? I got the compile error!

            typedef unsigned char BYTE;

            void foo(char* & p)
            {
            return;
            }

            int main()
            {
            BYTE * buffer;
            // error C2664: 'foo' : cannot convert parameter 1 from 'char *' to 'char *&'
            foo ((char*)buffer);

            return 0;
            

            }

            thanks in advance, George

            _ Offline
            _ Offline
            _Superman_
            wrote on last edited by
            #5

            You must call it as below

            char* pp = (char*)buffer;
            foo(pp);

            «_Superman_»

            G 1 Reply Last reply
            0
            • R Rolf Kristensen

              BYTE is an unsigned char (8 bit) char* is a pointer (32 bit) When converting BYTE to char* then it needs to create a temporary variable. It is not possible to make return-by-reference with temporary variables. Instead make the conversion from BYTE to char* by using a local variable, and then pass that local variable to the function.

              G Offline
              G Offline
              George_George
              wrote on last edited by
              #6

              I agree with your points, but confused. Since I am using BYTE* not BYTE. Anyway, could you show me your code please? I think code clarifies everything. :-) regards, George

              1 Reply Last reply
              0
              • _ _Superman_

                You must call it as below

                char* pp = (char*)buffer;
                foo(pp);

                «_Superman_»

                G Offline
                G Offline
                George_George
                wrote on last edited by
                #7

                Your code fixes my issue, but why? I think both your code and my code are of the same effect. :-) regards, George

                _ 1 Reply Last reply
                0
                • G George_George

                  Your code fixes my issue, but why? I think both your code and my code are of the same effect. :-) regards, George

                  _ Offline
                  _ Offline
                  _Superman_
                  wrote on last edited by
                  #8

                  When you typecast, you have to give the destination exactly as it should be. So you could also typecast if as foo((char*& )buffer)

                  «_Superman_»

                  1 Reply Last reply
                  0
                  • R Rolf Kristensen

                    BYTE is an unsigned char (8 bit) char* is a pointer (32 bit) When converting BYTE to char* then it needs to create a temporary variable. It is not possible to make return-by-reference with temporary variables. Instead make the conversion from BYTE to char* by using a local variable, and then pass that local variable to the function.

                    G Offline
                    G Offline
                    grassrootkit
                    wrote on last edited by
                    #9

                    Where is he converting a BYTE to char*? He's converting a BYTE* to char*. In other words, unsigned char* to char*. I think there's no problem with the pointer size. this is enough: foo ((char*&)buffer);

                    S 1 Reply Last reply
                    0
                    • G grassrootkit

                      Where is he converting a BYTE to char*? He's converting a BYTE* to char*. In other words, unsigned char* to char*. I think there's no problem with the pointer size. this is enough: foo ((char*&)buffer);

                      S Offline
                      S Offline
                      Stuart Dootson
                      wrote on last edited by
                      #10

                      It's all to do with the semantics of casting - you can't pass a reference to something you've produced with a cast, even if the size and location of the thing is the same.

                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                      1 Reply Last reply
                      0
                      • G George_George

                        Hello everyone, Anything wrong with my code below? I got the compile error!

                        typedef unsigned char BYTE;

                        void foo(char* & p)
                        {
                        return;
                        }

                        int main()
                        {
                        BYTE * buffer;
                        // error C2664: 'foo' : cannot convert parameter 1 from 'char *' to 'char *&'
                        foo ((char*)buffer);

                        return 0;
                        

                        }

                        thanks in advance, George

                        S Offline
                        S Offline
                        Stuart Dootson
                        wrote on last edited by
                        #11

                        It's all to do with the semantics of casting - you can't pass a non-const reference to something you've produced with a cast, even if the size and location of the thing is the same.

                        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups