Go Down

Topic: #include file with an array? (Read 300 times) previous topic - next topic

SergeantBiscuits

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

patduino

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
There are 10 types of people in the world, those that understand binary, and those that don't.

WizenedEE

file1.ino
Code: [Select]

#include "myneatarray.c"

void setup() {}
void loop(){}

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

this should also work:
file1.ino
Code: [Select]

int ary[] = {
#include "myneatarray.c"
};
void setup() {}
void loop(){}

myneatarray.c (in same directory)
Code: [Select]
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.

Go Up