Advertisement

array

Started by April 29, 2001 05:15 PM
8 comments, last by Thalion 23 years, 9 months ago
if I have an array like this: char map[2][2] = { 1,1, 1,1, }; how do I give it as an argument to an function?
  void MyFunction(char **map) {  /* DO STUFF HERE */}char map[2][2] = { 1,1,1,1 };MyFunction(map);  


Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/
Advertisement
If your going to use the exact array size all the time, you might want to use a typedef like so...

#include

typedef char MAT22[2][2];

char Determinant(MAT22 M)
{
return M[0][0]*M[1][1] - M[1][0]*M[0][1];
}

void main()
{
MAT22 Dude;
Dude[0][0] = 1;
Dude[0][1] = -1;
Dude[1][0] = 2;
Dude[1][1] = 3;

printf("The det is %d.\n", int(Determinant(Dude)));
}
Hmm, I cant make it to work.

Im trying to do this after two for-loopes:
if (map[j] == 1)

and I get this error message: cannot convert parameter from char[2][2] to char **
It´s very late here in Spain, so just before I go to bed...

Null and Void your code DOESN´T work.

I had that problem a week or so ago and it doesn´t work.

Tomorrow I´ll try to remember the solution I found.

Please write a small program to test the solutions because it isn´t obvious.

What the hells!
What the hells!
Actually, it is his code that I senslessly didn't correct all the way that doesn't work:
    void MyFunction(char **map) {  /* DO STUFF HERE */}char map[2][2] = { {1,1}, {1,1} };MyFunction(map);    


Resist Windows XP's Invasive Production Activation Technology!
http://druidgames.cjb.net/

Edited by - Null and Void on April 29, 2001 7:04:55 PM
Advertisement
It still doesnt work. I get the same error message.
you simply can''t send a multidimentional array to a function without knowing its dimentions. This may work

void MyFunction(char map[2][2]) {
/* DO STUFF HERE */
}

but you wont be able to change the size of the array

#include "stdio.h"
#include "conio.h"

void MyFunc(char asd[2][2])
{
printf("%d %d %d %d",(int)asd[0][0],(int)asd[0][1],(int)asd[1][0],(int)asd[1][1]);
}


void main(void)
{
char blah[2][2]={{1,2},{3,4}};

MyFunc(blah);
getch();
}

This above works and this is what i beleive to be true anyway.
If you have to sent multidimentional char arrays, you could always sen the dimentions as ints and a char * but you''d have to do some pointer manipulation. Please correct me if this is a load of crap.
Just cast it as a char **, the compiler is being overly sensitive.

Resist Windows XP''s Invasive Production Activation Technology!
http://druidgames.cjb.net/
Yes, casting to a char** will work. I was having that exact problem last night, except with a 1D array. Seems rather ridiculous that it wouldn''t be able to automatically convert from char[256] to char*, but I guess there''s not much to be done about it.



-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)

This topic is closed to new replies.

Advertisement