Go Down

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


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!



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:


You can pass flash-memory based strings to Serial.print() by wrapping them with F(). For example :
Serial.print(F("Hello World"))

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


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:
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

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131