Pages: [1]   Go Down
Author Topic: #include file with an array?  (Read 248 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey guys, quick question--

I'm writing some video game code that will have a long list of weapon names stored as strings that I want to select by an index.

For instance, weaponName[0] = "Heavy Machinegun", weaponName[1] = "Gauss Rifle", etc.

I could declare all the contents in the code, but I'm wondering if there's a way to save some real estate by having the array contents in an #included file.

If so, what is the procedure for implementing such a thing?

Thank you very much!

Wes
Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi Sergeant,

Nope, don't put "executable" code like arrays in header files, just definitions and function prototypes.

It won't save space, either.  You should consider storing the arrays in flash memory.  See the following links:

http://arduino.cc/en/Tutorial/Memory

http://arduino.cc/en/Serial/Print?from=Reference.PrintHex
Quote
You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :
Serial.print(F(“Hello World”))

Pat
Logged

There are 10 types of people in the world, those that understand binary, and those that don't.

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

file1.ino
Code:
#include "myneatarray.c"

void setup() {}
void loop(){}
myneatarray.c (in same directory)
Code:
int ary[] = {3, 1, 4, 1, 5, 9};

this should also work:
file1.ino
Code:
int ary[] = {
#include "myneatarray.c"
};
void setup() {}
void loop(){}
myneatarray.c (in same directory)
Code:
3, 1, 4, 1, 5, 9

#include just has something run before the compiler insert the one file into the other. It doesn't care about anything else.
Logged

Pages: [1]   Go Up
Jump to: