Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - Pointer to multidimensional array

 
Thread Tools Search this Thread
Old 06-03-2004, 01:42 PM   #1
Default Pointer to multidimensional array


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
  Reply With Quote
Old 06-03-2004, 02:06 PM   #2
Gianni Mariani
 
Posts: n/a
Default Re: Pointer to multidimensional array
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
  Reply With Quote
Old 06-04-2004, 01:15 AM   #3
shane
 
Posts: n/a
Default Re: Pointer to multidimensional array
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
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

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




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46