Author Topic: Passing a pointer to an array of an array  (Read 3586 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Passing a pointer to an array of an array
« on: October 22, 2011, 05:46:06 am »
I need some help with a pointer problem I'm facing. I have this:

Code: [Select]
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?

Code: [Select]
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!

Offline thepenguin77

  • z80 Assembly Master
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1594
  • Rating: +823/-5
  • The game in my avatar is bit.ly/p0zPWu
    • View Profile
Re: Passing a pointer to an array of an array
« Reply #1 on: October 22, 2011, 11:20:04 am »
That might work, but why not save all the hassle and do this?

Code: [Select]
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.
« Last Edit: October 22, 2011, 11:20:30 am by thepenguin77 »
zStart v1.3.013 9-20-2013 
All of my utilities
TI-Connect Help
You can build a statue out of either 1'x1' blocks or 12'x12' blocks. The 1'x1' blocks will take a lot longer, but the final product is worth it.
       -Runer112

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Passing a pointer to an array of an array
« Reply #2 on: October 22, 2011, 01:21:36 pm »
That might work, but why not save all the hassle and do this?

Code: [Select]
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 ;)