473,383 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

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.
Jul 22 '05 #1
2 6936
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.

Jul 22 '05 #2
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]);
}
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: overbored | last post by:
I can do this: int asdf; int* zxcv = asdf; but not this: int asdf; int** zxcv = asdf;
4
by: Kobu | last post by:
I've read the FAQ and several posts on multidimensional arrays and how their names decay to pointer to arrays (not pointer to pointers). If this is so, why does the following code fragment...
3
by: S. Nurbe | last post by:
Hi, small question concerning pointer: How do I allocate and have access to this pointer in C: float **x; such that I can work with it like an array, e.g. x?
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
14
by: sheroork | last post by:
Can anyone help me in getting to understand pointer to pointer with examples? Appritiate in advance. Sagar
24
by: Kavya | last post by:
int main (){ int a={{1,2,3},{4,5,6}}; int (*ptr)=a; /* This should be fine and give 3 as output*/ printf("%d\n",(*ptr)); ++ptr;
26
by: Ioannis Vranos | last post by:
Are the following guaranteed to work always as *C90* code? 1. #include <stdio.h> void some_func(int *p, const size_t SIZE) { size_t i;
14
by: Szabolcs Borsanyi | last post by:
Deal all, The type typedef double ***tmp_tensor3; is meant to represent a three-dimensional array. For some reasons the standard array-of-array-of-array will not work in my case. Can I...
0
by: Szabolcs Borsanyi | last post by:
Dear All, there have been several threads on multidimensional arrays and pointers to arrays, there is still something I could not fully understand. (My point here I have raised already, but...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.