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:
of course this is just an example and doesn't work :P
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...
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
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?)
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?)
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;
}
Whoops, change the line that prints the value to this:
Code:
printf("%i", array[x*sizeY + y]);
GO MAEK A DEMO BUOT IT HARHARHARHARHARHAR
is this a homework :O?
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.
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
or even
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...}
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.
This is why the [][] are on the variable side.
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.
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");
}
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");
}
sidenote: I still learn new things about C in 2012? :)