Alex Rivera | Logout

Declare array in C++ header and define it in cpp file?

Asked 2010-12-08T19:31:48.523
40

This is probably a really simple thing but I'm new to C++ so need help.

I just want to declare an array in my C++ header file like:

int lettersArr[26];

and then define it in a function in the cpp file like:

    lettersArr[26] = { letA, letB, letC, letD, letE, letF, letG, letH,
        letI, letJ, letK, letL, letM, letN, letO, letP, letQ, letR, letS,
        letT, letU, letV, letW, letX, letY, letZ };

but this doesn't work.

Have I got the syntax wrong or something? What is the correct way to to this?

Thanks a lot.

Edit
Report

2 Answers

4

Header:

extern int lettersArr[];

Source at the global scope:

int lettersArr[26] = { letA, letB, letC, letD, letE, letF, letG, letH,
    letI, letJ, letK, letL, letM, letN, letO, letP, letQ, letR, letS,
    letT, letU, letV, letW, letX, letY, letZ };

or if you really want to do it in a function:

Source at global scope:

int lettersArr[26];

Source in function:

int localLettersArr[26] = { letA, letB, letC, letD, letE, letF, letG, letH,
    letI, letJ, letK, letL, letM, letN, letO, letP, letQ, letR, letS,
    letT, letU, letV, letW, letX, letY, letZ };

memcpy (lettersArr, localLettersArr, sizeof (localLettersArr));
answered 2010-12-08T19:38:22.410
-2

try:

lettersArr = { 1, 2, 3, 4 }
answered 2010-12-08T19:41:37.853

Your Answer