Need help with error C2664
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Need help with error C2664

  1. #1
    Join Date
    Jun 2011
    Posts
    30

    Need help with error C2664

    I have a question as to why the two shown func args are different? See comments (yes I am novice)

    struct MyFileClass
    {
    .....other code

    void MyFileOut( TCHAR* pS, DWORD pVar)
    {
    TCHAR PrntStrg1[] = _T("The value of passed ptr->");
    .... other code

    MyFilePrint(&PrntStrg1, Scratch_PP); //<-this pulls error C2664:
    //cannot convert parameter 1 from 'TCHAR (*__w64 )[26]' to 'TCHAR *'

    MyFilePrint(PrntStrg1, Scratch_PP); //<-this does not
    // doesn't PrntStrg1 and &PrntStrg1 evaluate to the same thing ?

    ......other code
    } // end MyFileOut

    int MyFilePrint(TCHAR* pS, DWORD pVar)
    {
    ....other code
    } // end MyFilePrint

    } // end MyFileClass

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Need help with error C2664

    The type of PrntStrg1 is TCHAR[] which is (almost) the same as TCHAR*.
    The type of PrntStrg1* is TCHAR*[] which is (almost) the same as TCHAR**.

    Do you see the difference?
    Victor Nijegorodov

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,013

    Re: Need help with error C2664

    Quote Originally Posted by VictorN View Post
    The type of PrntStrg1* is TCHAR*[] which is (almost) the same as TCHAR**.
    I wouldn't say they are almost the same, because they are not convertible to each other.
    Code:
    void foo(char** test);
    void bar()
    {
      char array[] = "test";
      foo(&array); // error C2664: 'foo' : cannot convert parameter 1 from 'char (*)[5]' to 'char **'
    }
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Need help with error C2664

    Quote Originally Posted by D_Drmmr View Post
    I wouldn't say they are almost the same, because they are not convertible to each other.
    Code:
    void foo(char** test);
    void bar()
    {
      char array[] = "test";
      foo(&array); // error C2664: 'foo' : cannot convert parameter 1 from 'char (*)[5]' to 'char **'
    }
    Yes, you are right.
    I just tried to explain the OP problem as short as possible... So sorry for non-correct explanation!
    Victor Nijegorodov

  5. #5
    Join Date
    Jun 2011
    Posts
    30

    Re: Need help with error C2664

    Quote Originally Posted by VictorN View Post
    The type of PrntStrg1 is TCHAR[] which is (almost) the same as TCHAR*.
    The type of PrntStrg1* is TCHAR*[] which is (almost) the same as TCHAR**.
    Do you see the difference?
    Quote Originally Posted by D_Drmmr View Post
    I wouldn't say they are almost the same, because they are not convertible to each other.
    Code:
     
    void foo(char** test);
    void bar()
    {
      char array[] = "test";
      foo(&array); // error C2664: 'foo' : cannot convert parameter 1 from 'char (*)[5]' to 'char **'
    }
    Quote Originally Posted by VictorN View Post
    Yes, you are right.
    I just tried to explain the OP problem as short as possible... So sorry for non-correct explanation!
    Thanks for the explanations. I kinda see what your are taking about. I guess I need to do some experimental evaluations to make it more clear to me. I had always thought an Char array was the same as the address of the first character, and that in essense it was the same as Char ptr. So I guess you are saying that I'm passing the address of the ptr instead of the ptr. Anyhow at least you guys have shown me where I have some learning to do. I thank you much.

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,392

    Re: Need help with error C2664

    Quote Originally Posted by J_W View Post
    ... So I guess you are saying that I'm passing the address of the ptr instead of the ptr.
    Exactly!
    Victor Nijegorodov

  7. #7
    Join Date
    Apr 1999
    Posts
    27,427

    Re: Need help with error C2664

    Quote Originally Posted by J_W View Post
    I had always thought an Char array was the same as the address of the first character, and that in essense it was the same as Char ptr.
    An array is an array (a contiguous buffer of a certain type), and a pointer is a pointer. They are not the same thing.

    What you're confused about is that

    1) a pointer can be used syntactically to traverse, retrieve, or set an element within an array

    2) When you "pass an array" to a function, the receiving function receives a pointer to the first element of the array. Then item 1) takes effect within the function.

    But do not make the mistake that an array is a pointer and a pointer is an array. If you want further proof:
    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    {
       char x[100];
       char* y;
       cout << sizeof(x) << endl;
       cout << sizeof(y) << endl;
    }
    What values are printed when I attempt to retrieve the size of each of those types? I can bet they are vastly different.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Jun 2011
    Posts
    30

    Re: Need help with error C2664

    Quote Originally Posted by Paul McKenzie View Post
    . . . . .
    What you're confused about is that

    1) a pointer can be used syntactically to traverse, retrieve, or set an element within an array

    2) When you "pass an array" to a function, the receiving function receives a pointer to the first element of the array. Then item 1) takes effect within the function.

    But do not make the mistake that an array is a pointer and a pointer is an array. If you want further proof:
    Code:
    #include <iostream>
    
    using namespace std;
    int main()
    {
       char x[100];
       char* y;
       cout << sizeof(x) << endl;
       cout << sizeof(y) << endl;
    }
    Regards, Paul McKenzie
    Point understood, thank you. 100 bytes compared to 4.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center