![]() |
|
|
|||||||
![]() |
C++ - Pointer to multidimensional array |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
Ive searched a fair bit for the answer, but nothing has come up that
matches what i want to do. I'm having an issue with passing and assigning pointers to multidimensional arrays. The code: /*.....*/ TCHAR msg[2][4][MAX_PATH] func(msg); /*.....*/ func(TCHAR *pmsg[2][4][MAX_PATH]) { TCHAR *m_pmesg; m_pmesg = pmsg; } Ive tried a few different variations but with no luck. I keep getting the error error C2440: '=' : cannot convert from 'TCHAR *[][2][260]' to 'TCHAR *[4][2][260]' on the line 'm_pmesg = pmsg' as well as 'func(msg)'. I would appreciate any help. shane |
|
|
|
|
#2 |
|
Posts: n/a
|
shane wrote:
> Ive searched a fair bit for the answer, but nothing has come up that > matches what i want to do. I'm having an issue with passing and > assigning pointers to multidimensional arrays. > > The code: > > /*.....*/ > TCHAR msg[2][4][MAX_PATH] > func(msg); > /*.....*/ > > func(TCHAR *pmsg[2][4][MAX_PATH]) You ca do either: func(TCHAR pmsg[2][4][MAX_PATH]) or func(TCHAR pmsg[][4][MAX_PATH]) or func(TCHAR (&pmsg)[2][4][MAX_PATH]) > { > TCHAR *m_pmesg; > m_pmesg = pmsg; > } > > Ive tried a few different variations but with no luck. I keep getting > the error > > error C2440: '=' : cannot convert from 'TCHAR *[][2][260]' to 'TCHAR > *[4][2][260]' This is because an array becomes a pointer when it is passed to a function (unless it's passed by reference). Arrays are never passed by value (unless they are in a struct or class). e.g. void func( char i[] ); // (essentially the same as func( char * i ) ) char v[30]; func( v ); However, if you want to maintain all array information, you can pass by reference. void func( char (&i)[30] ); func( v ); > > on the line 'm_pmesg = pmsg' as well as 'func(msg)'. m_pmesg is a pointer to a element of the array but you're trying to assign it a pointer to the entire array. Hence you get the message of type mismatch. const int MAX_PATH = 200; char msg[2][4][MAX_PATH]; void func(char pmsg[2][4][MAX_PATH]) { char * m_pmesg; m_pmesg = pmsg[0][0]; } int main() { func( msg ); } > I would appreciate any help. Gianni Mariani |
|
|
|
#3 |
|
Posts: n/a
|
Thanks for your help, though I did actually fix it not long after
posting I used the below code, note the '(*)'. All I needed was some paranthese around the *, though I don't really understand why to be honest. I also needed a '&' when passing the array for some reason. As you said, arrays are always passed by reference so Im not sure why thats needed. /*.....*/ TCHAR msg[2][4][MAX_PATH] func(&msg); /*.....*/ func(TCHAR (*)pmsg[2][4][MAX_PATH]) { m_pmsg = pmsg; printf(*m_pmsg[1][2]); } shane |
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| constants as of array of integers, for loops | octavsly | Hardware | 0 | 04-25-2009 11:53 AM |
| How to retrieve array parameter ( JAVA ) | naruponk | Software | 1 | 04-16-2009 10:20 AM |
| Array Programme | rits | Software | 2 | 03-04-2009 05:18 PM |
| Null Pointer | rits | General Help Related Topics | 0 | 02-12-2009 04:31 PM |
| Mouse pointer disappear in e-mail subject ?? | zhongnaomi | General Help Related Topics | 1 | 11-05-2008 10:57 PM |