|
Re: multidimensional arrays as function arguments
[Re: Kartoffel]
#429377
09/11/13 20:07
09/11/13 20:07
|
Joined: Nov 2007
Posts: 318 Brasil, Paraná
NeoNeper
Senior Member
|
Senior Member
Joined: Nov 2007
Posts: 318
Brasil, Paraná
|
Now I understand where is the problem. and also can not solve
Please! Use easy words to be translated. because my English is not very good! Grateful. _______________________________________________________
|
|
|
Re: multidimensional arrays as function arguments
[Re: Kartoffel]
#429381
09/11/13 20:36
09/11/13 20:36
|
Joined: Apr 2006
Posts: 159 Latvija
Arrovs
Member
|
Member
Joined: Apr 2006
Posts: 159
Latvija
|
As im never at need after constant size arrays i can give you something like this:
#include <acknex.h>
//for new struct allocating
#define new(struktura) sys_malloc(sizeof(struktura))
#define newm(struktura, daudzums) sys_malloc(daudzums*sizeof(struktura))
//no matter which type as any pointer have same size except void* which will nto work here
typedef unsigned int* pointer;
//Array for any pointers you need
typedef struct Array
{
pointer values;
int length;
}Array;
//simple array control
Array* Array_inic(int length)
{
Array* lok_array = new(Array);
lok_array.values = newm(pointer, length);
lok_array.length = length;
return lok_array;
}
Array Array_del(Array* array)
{
sys_free(array.values);
sys_free(array);
}
void print_var(Array *input_array, unsigned int index_x, unsigned int index_y)
{
Array* lok_array = (Array*)(input_array.values)[index_x];
var* output_var = (lok_array.values)[index_y];
printf("array[%.0f][%.0f] = %.0f", (double)index_x, (double)index_y, (double)*output_var);
}
void main()
{
randomize();
Array* vars = Array_inic(8);
int i;
int j;
for(i = 0; i <= 8; i++)
{
(vars.values)[i] = Array_inic(8);
for(j = 0; j <= 8; j++)
{
var* lok_var = new(var);
*lok_var = random(100);
Array* lok_array = (vars.values)[i];
(lok_array.values)[j] = lok_var;
}
}
wait(1);
print_var(vars, 4, 4);
}
As this struct is pointer array every value need to be pointer too, but you can define specific things as 2darray struct or even only function for that. Overall i was amazed about 2d array not working through parameter.
Last edited by Arrovs; 09/11/13 20:44.
Arrovs once will publish game
|
|
|
Re: multidimensional arrays as function arguments
[Re: Arrovs]
#429384
09/11/13 20:58
09/11/13 20:58
|
Joined: Apr 2006
Posts: 159 Latvija
Arrovs
Member
|
Member
Joined: Apr 2006
Posts: 159
Latvija
|
Although overall i use this method, but as im more often running into need after knowing how long is my arrays im started to make upper mentioned struct.
void*** masivs_2d(int garums, int augstums)
{
//pirmā dimensija
void** masivs = jauni(void*, garums);
//otrās dimensijas
int lok_i = 0;
for(lok_i = 0; lok_i < garums; lok_i++)
{
(masivs)[lok_i] = jauni(void*, augstums);
}
return (void***)masivs;
}
function masivs_2d_dzest(void*** masivs, garums)
{
int lok_i = 0;
for(lok_i = 0; lok_i < garums; lok_i += 1)
{
sys_free((masivs)[lok_i]);
}
sys_free(masivs);
return NULL;
}
void print_var(void*** input_array, BOOL index_x, BOOL index_y)
{
var* output_var = ((lok_array)[index_x])[index_y];
printf("array[%.0f][%.0f] = %.0f", (double)index_x, (double)index_y, (double)*output_var);
}
function main()
{
vars* vars = masivs_2d(8, 8);
//fill it up, but still it is pointer array
print_var(vars, 4, 4)
}
Arrovs once will publish game
|
|
|
Re: multidimensional arrays as function arguments
[Re: sivan]
#429410
09/12/13 09:35
09/12/13 09:35
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
OP
Expert
|
OP
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
@sivan: yes and no. it's not the reason for the 'dimension of array'-error, but if it worked the i++ would have caused a script error.
POTATO-MAN saves the day! - Random
|
|
|
Re: multidimensional arrays as function arguments
[Re: Kartoffel]
#429419
09/12/13 11:10
09/12/13 11:10
|
Joined: Mar 2012
Posts: 927 cyberspace
Wjbender
User
|
User
Joined: Mar 2012
Posts: 927
cyberspace
|
your code works 100% when i tested it (except i changed your i++) but with or without i it i had no errors even after passing addres of vars or ommiting the pointer it still worked fine .
what error are you receiving exactly ?
i am using a8 free and no errors whatsoever...
im guessing you receive a compiler error ?
m
Compulsive compiler
|
|
|
Re: multidimensional arrays as function arguments
[Re: Wjbender]
#429422
09/12/13 11:17
09/12/13 11:17
|
Joined: Nov 2007
Posts: 318 Brasil, Paraná
NeoNeper
Senior Member
|
Senior Member
Joined: Nov 2007
Posts: 318
Brasil, Paraná
|
#include <acknex.h>
var vars[8][8];
void print_var(var **input_array, unsigned int index_x, unsigned int index_y)
{
var output_var = input_array[index_x][index_y];
printf("array[%.0f][%.0f] = %.0f", (double)index_x, (double)index_y, (double)output_var);
}
void main()
{
int i;
int j;
for(i = 0; i <= 8; i++)
for(j = 0; j <= 8; j++)
{
vars[i][j] = 100;
}
wait(1);
print_var(vars, 8, 8);
}
NOOO WOOORK. The problem is to pass as parameter the address of a variable dimensional Look: void print_var( var **input_array, unsigned int index_x, unsigned int index_y) { var output_var = input_array[index_x][index_y]; ... This is where the problem is! How can be indicated by parameters the address of a variable BI-Dimensional
Last edited by NeoNeper; 09/12/13 11:23.
Please! Use easy words to be translated. because my English is not very good! Grateful. _______________________________________________________
|
|
|
Re: multidimensional arrays as function arguments
[Re: Wjbender]
#429426
09/12/13 11:33
09/12/13 11:33
|
Joined: Jun 2009
Posts: 2,210 Bavaria, Germany
Kartoffel
OP
Expert
|
OP
Expert
Joined: Jun 2009
Posts: 2,210
Bavaria, Germany
|
your code works 100% when i tested it (except i changed your i++) but with or without i it i had no errors even after passing addres of vars or ommiting the pointer it still worked fine .
what error are you receiving exactly ?
i am using a8 free and no errors whatsoever...
im guessing you receive a compiler error ?
m I'm using A8 Pro (before you ask: I know, pirated versions contain lots of bugs but mine is legal) thats the code and compiler error: @NeoNeper: the same as above is happening ;_; I guess I'll have to try to reinstall 3DGS
POTATO-MAN saves the day! - Random
|
|
|
Re: multidimensional arrays as function arguments
[Re: Kartoffel]
#429429
09/12/13 11:40
09/12/13 11:40
|
Joined: Nov 2007
Posts: 318 Brasil, Paraná
NeoNeper
Senior Member
|
Senior Member
Joined: Nov 2007
Posts: 318
Brasil, Paraná
|
The problem is not the 3DGS, as you and I reported above, this problem really exists ..
Is it not the case that developers ask?
Please! Use easy words to be translated. because my English is not very good! Grateful. _______________________________________________________
|
|
|
|