-
April 24th, 2013, 10:29 PM
#1
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
-
April 25th, 2013, 01:23 AM
#2
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
-
April 25th, 2013, 01:56 AM
#3
Re: Need help with error C2664
Originally Posted by VictorN
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
-
April 25th, 2013, 02:59 AM
#4
Re: Need help with error C2664
Originally Posted by D_Drmmr
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
-
April 25th, 2013, 07:12 AM
#5
Re: Need help with error C2664
Originally Posted by VictorN
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?
Originally Posted by D_Drmmr
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 **'
}
Originally Posted by VictorN
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.
-
April 25th, 2013, 07:17 AM
#6
Re: Need help with error C2664
Originally Posted by J_W
... So I guess you are saying that I'm passing the address of the ptr instead of the ptr.
Exactly!
Victor Nijegorodov
-
April 25th, 2013, 11:21 AM
#7
Re: Need help with error C2664
Originally Posted by J_W
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
-
April 25th, 2013, 11:49 AM
#8
Re: Need help with error C2664
Originally Posted by Paul McKenzie
. . . . .
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|