Omnimaga: The Coders Of Tomorrow
Welcome, Guest. Please login or register.
 
Omnimaga: The Coders Of Tomorrow
20 May, 2013, 05:52:15 *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   home   news downloads projects tutorials misc forums rules new posts irc about Login Register  
+-OmnomIRC

You must Register, be logged in and have at least 40 posts to use this shout-box! If it still doesn't show up afterward, it might be that OmnomIRC is disabled for your group or under maintenance.

Note: You can also use an IRC client like mIRC, X-Chat or Mibbit to connect to an EFnet server and #omnimaga.

Pages: [1]   Go Down
  Print  
Author Topic: Passing a pointer to an array of an array -  (Read 260 times) Bookmark and Share
0 Members and 1 Guest are viewing this topic.
Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: 07 May, 2013, 22:49:01
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« on: 22 October, 2011, 11:46:06 »
0

I need some help with a pointer problem I'm facing. I have this:


1
2
3
4
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?


1
2
3
4
5
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!
Logged
thepenguin77
z80 Assembly Master
LV10 31337 u53r (Next: 2000)
**********
Offline Offline

Gender: Male
Last Login: Today at 04:29:33
Date Registered: 14 December, 2009, 04:21:52
Location: Purdue
Posts: 1484


Total Post Ratings: +778

View Profile
« Reply #1 on: 22 October, 2011, 17:20:04 »
+1

That might work, but why not save all the hassle and do this?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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: 22 October, 2011, 17:20:30 by thepenguin77 » Logged

zStart v1.3.011 4-29-2013  zStart fully works on 83+BE's (except custom font)
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
Munchor
LV13 Extreme Addict (Next: 9001)
*************
Offline Offline

Gender: Male
Last Login: 07 May, 2013, 22:49:01
Date Registered: 16 October, 2010, 15:39:13
Location: Position
Posts: 6209


Topic starter
Total Post Ratings: +174

View Profile
« Reply #2 on: 22 October, 2011, 19:21:36 »
0

That might work, but why not save all the hassle and do this?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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 Wink
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Powered by MySQL Powered by SMF 1.1.18 | SMF © 2013, Simple Machines Powered by PHP
Page created in 0.261 seconds with 32 queries.
Skin by DJ Omnimaga edited from SMF default theme with the help of tr1p1ea.
All programs, games and songs avaliable on this website are property of their respective owners.
Best viewed in Opera, Firefox, Chrome and Safari with a resolution of 1024x768 or above.