Omnimaga
General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on October 22, 2011, 05:46:06 am
-
I need some help with a pointer problem I'm facing. I have this:
int my_array[26][24]; /* An 2 dimensions array */
call_function(NEED_HELP_HERE);
call_function() requires an argument, an array. I need to pass my_array. However, I know that passing arrays in functions is memory consuming and not very clean. So, instead, I want to pass a pointer to the array.
So my question is how to declare the pointer to the array, how do I declare it?
void call_function(NEED_HELP_HERE)
{
/* This function receives a pointer to an array as argument */
}
The question is what to put in "NEED_HELP_HERE" places. My issue is that I don't know what type the pointer to the array of arrays is.
Thanks in advance!
-
That might work, but why not save all the hassle and do this?
int my_array[26][24] = {0};
call_function(my_array);
////////////////////////////////
void call_function(int array[26][24])
{
for (int iii = 0; iii<26; iii++)
{
for (int jjj = 0; jjj<24; jjj++)
{
array[iii][jjj] = iii*jjj;
}
}
}
When you just type the name of an array, like "my_array," that actually is the pointer to the array. You only dereference it when you start to use [].
Also, I would suggest that you use a 1 dimensional array. Two dimensional arrays are just handled like 1 dimensional arrays in memory anyways.
-
That might work, but why not save all the hassle and do this?
int my_array[26][24] = {0};
call_function(my_array);
////////////////////////////////
void call_function(int array[26][24])
{
for (int iii = 0; iii<26; iii++)
{
for (int jjj = 0; jjj<24; jjj++)
{
array[iii][jjj] = iii*jjj;
}
}
}
When you just type the name of an array, like "my_array," that actually is the pointer to the array. You only dereference it when you start to use [].
Also, I would suggest that you use a 1 dimensional array. Two dimensional arrays are just handled like 1 dimensional arrays in memory anyways.
Oh ooops, I forgot arrays are just pointers, how silly of me, thanks for reminding me ;)