Pages: [1]   Go Down
Author Topic: pointer array?  (Read 505 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Edison Member
*
Karma: 8
Posts: 1249
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi gang-

I have a question on using arrays.

normal usage:

byte buttons[] = {5, 6, 3, 9};

can it be done like this instead:

const int safetyPin =  5;
const int normalPin = 6;
const int autoPin =  3;
const int reloadPin =  9;

byte buttons[] = {safetyPin, normalPIn, autoPin, reloadPin};


I guess an arrays of variables?  or references to pin(s)?
this way I can also refer to them by pin name else where..etc..

Thanks
Logged


North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 70
Posts: 2171
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes

Code:
int *i_Array[] = { &safetyPin, &normalPIn, &autoPin, &reloadPin};

notice the int pointer type, not a byte, declare the constants as bytes to use byte*
EDIT: this will use more ram than your first method.
« Last Edit: July 31, 2012, 09:30:20 pm by pYro_65 » Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18800
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

can it be done like this instead:

Code:
const int safetyPin =  5;
const int normalPin = 6;
const int autoPin =  3;
const int reloadPin =  9;

byte buttons[] = {safetyPin, normalPIn, autoPin, reloadPin};


Try it and see? Apart from the typo on normalPIn, that compiles and should work.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18800
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Even this, since you are using constants:

Code:
const byte buttons[] = {safetyPin, normalPin, autoPin, reloadPin};
Logged


Offline Offline
God Member
*****
Karma: 19
Posts: 781
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

But the way you and Nick are doing it isn't a pointer.  You're just setting one variable (in the array) equal to another (not in the array).  There aren't really any pointers involved here.

The code Pyro posted uses pointers and would involve some other changes in your code to dereference those.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18800
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The OP did not say anything about pointers.
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18800
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Ah, except the thread title of course.

My response was to the actual post, I ignored the thread title.
Logged


Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4807
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi gang-

I have a question on using arrays.

normal usage:

byte buttons[] = {5, 6, 3, 9};

can it be done like this instead:

const int safetyPin =  5;
const int normalPin = 6;
const int autoPin =  3;
const int reloadPin =  9;

byte buttons[] = {safetyPin, normalPIn, autoPin, reloadPin};


I guess an arrays of variables?  or references to pin(s)?
this way I can also refer to them by pin name else where..etc..

Thanks

Learn about enumerators (enum), they can also help you keep your code straighter.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

0
Offline Offline
Edison Member
*
Karma: 8
Posts: 1249
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks guys..

whre can I learn about:  (enum)?

I didnt see it listed here:  http://arduino.cc/en/Reference/HomePage

Am I allowed to use any "C" code or something?

I found this:
http://arduino.cc/playground/Code/Enum

but how to use it and why didnt really click yet.. smiley-cry
Logged


Offline Offline
God Member
*****
Karma: 19
Posts: 781
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Google up.any good C++ tutorial.  Enum is a pretty basic piece.  Shouldn't be hard to find a good explanation. 
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 70
Posts: 2171
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your intention could be expressed using enums something like this:

Code:
enum MY_PIN_SET{
  safetyPin =  5,
  normalPin = 6,
  autoPin =  3,
  reloadPin =  9,
};

const byte buttons[] = {safetyPin, normalPin, autoPin, reloadPin};
Logged


Pages: [1]   Go Up
Jump to: