pouët.net

Go to bottom

Parse multidimensional arrays to functions [c++]

category: code [glöplog]
 
So i was wondering if theres a easy and practical way to parse a multidimensional arrays to a function without changing the function, lets say i have two arrays with different dimensions, and i want to use them in the same fuction, how?
example:

Code: int array[2][4] = {{1,2,3,4},{4,5,6,7}} int array[3][2] = {{1,2},{3,4},{5,6}} //How to use both of those arrays in the same function without changing anything? function coolStoryBro(int arrayf[][],sizeX,sizeY) { //etc }

of course this is just an example and doesn't work :P
trye see if pointers work...
added on the 2012-04-15 03:24:46 by rudi rudi
rudi: sorry, i am a complete noob in c++, how?! D:
bah. first of all that should not compile. you cant have declare and define two arrays with the same name, second of all im too drubk to say more and bedtime
added on the 2012-04-15 03:43:03 by rudi rudi
it was an example, oh don't tell me i have to read my 500 pages c++ book again? pl0x halp D:

p0int3rz r h4rdz

(leet language makes everything better right?)
it was an example, oh don't tell me i have to read my 500 pages c++ book again? pl0x halp D:

p0int3rz r h4rdz

(leet language makes everything better right?)
Here's a program to illustrate:
Code: #include <stdio.h> int array1[2][4] = {{1,2,3,4},{4,5,6,7}}; int array2[3][2] = {{1,2},{3,4},{5,6}}; void printArray(int* array, int sizeX, int sizeY) { printf("{"); for (int x = 0; x < sizeX; x++) { printf("{"); for (int y = 0; y < sizeY; y++) { printf("%i", array[x + y*sizeX]); if (y != sizeY - 1) printf(","); } printf("}"); if (x != sizeX - 1) printf(","); } printf("}\n"); } int main(void) { printArray((int*)array1, 2, 4); printArray((int*)array2, 3, 2); return 0; }
added on the 2012-04-15 04:30:03 by xpansive xpansive
Whoops, change the line that prints the value to this:
Code:printf("%i", array[x*sizeY + y]);
added on the 2012-04-15 04:32:55 by xpansive xpansive
GO MAEK A DEMO BUOT IT HARHARHARHARHARHAR
added on the 2012-04-15 05:04:24 by ferris ferris
is this a homework :O?
added on the 2012-04-15 07:12:36 by panic panic
blacksheep: im sorry to break it to you but if you want to learn how to code graphics you need to first learn how to code. and when it comes to c++ if you dont know pointers properly you'll have plenty of headaches trying to figure stuff out.
added on the 2012-04-15 15:13:41 by psenough psenough
odd question: is it possible to cast a linear array as an n-dimensional array, or is xpansive's way the only way to do it?
something like
Code: if (sizeof(long) != 4) throw err("not long enough or too long"); void* stuff = malloc((480*800) << 2); *long[480][800] thing = null thing = (*(long[480][800]))stuff; violate_and_mess_with(thing); thing = null; free(stuff); //pun intended


or even

Code: void coolStoryBro(int* arrayf,int sizeX, int sizeY) { int[sizeX,sizeY] myway = *int[sizeX,sizeY](arrayf); //etc...}
added on the 2012-04-16 01:37:02 by QUINTIX QUINTIX
Quintix, you'll have a great problem doing that, because the C syntax when specifying a size for a local variable will always allocate memory.

Quote:

int array[5][9]; // this is not a type declaration but an actual allocation request


This is why the [][] are on the variable side.
added on the 2012-04-16 08:58:58 by _-_-__ _-_-__
Actually I found a C99 conforming version:

Quote:


void printItC99 (int sizeX, int sizeY, int array[sizeX][sizeY]) {
printf("{");
for (int x = 0; x < sizeX; x++) {
printf("{");
for (int y = 0; y < sizeY; y++) {
printf("%i", array[x][y]);
if (y != sizeY - 1)
printf(",");
}
printf("}");
if (x != sizeX - 1)
printf(",");
}
printf("}\n");
}
added on the 2012-04-16 09:02:12 by _-_-__ _-_-__
It actually works in c90 mode with gcc

Quote:


void printItC90 (int sizeX, int sizeY, int array[sizeX][sizeY]) {
int x, y;
printf("{");
for (x = 0; x < sizeX; x++) {
printf("{");
for (y = 0; y < sizeY; y++) {
printf("%i", array[x][y]);
if (y != sizeY - 1)
printf(",");
}
printf("}");
if (x != sizeX - 1)
printf(",");
}
printf("}\n");
}

added on the 2012-04-16 09:04:14 by _-_-__ _-_-__
sidenote: I still learn new things about C in 2012? :)
added on the 2012-04-16 09:05:06 by _-_-__ _-_-__

login

Go to top