how to define a constant array in c/c++?

Viewed 110082

How to define constant 1 or 2 dimensional array in C/C++? I deal with embedded platform (Xilinx EDK), so the resources are limited.

I'd like to write in third-party header file something like

#define MYCONSTANT 5

but for array. Like

#define MYARRAY(index) { 5, 6, 7, 8 }

What is the most common way to do this?

7 Answers
#include <string>
#include <iostream>
#define defStrs new string[4] { "str1","str2","str3","str4" }
using namespace std;
...

const string * strs = defStrs;
string ezpzStr = strs[0] + "test" + strs[1];

cout << ezpzStr << endl;

Took me a while to figure this out, but apparently it works like this in C++. Works on my computer anyway.

Only try to understand what the compiler does. When it finds the HASHTAG , it replaces its content into the place where it's writed. So for example, if u do this:

#define MYARRAY { 5, 6, 7, 8 }

byte list[] = MYARRAY ;

You will get it in the array 'list' . Another way to get the same result is:

#define MYARRAY 5, 6, 7, 8

byte list[] = {MYARRAY} ;

In conclusion, the compliler what does is 'cutting' the hashtag and 'pasting' the content of it. I hope I had solved your doubt

Related