A problem in declaring an array of 32-bit numbers

Hi folks,

I've done a fair bit of programming for Arduino, but have now been puzzled by a strange (to me) problem. Here are the details. First I try to declare an array of six 32-bit unsigned numbers:

unsigned long ADFReg[6];

Then I try to set the value of each array element, thus:

ADFReg[0] = 0x4580A8;
ADFReg[1] = 0x80080C9; and so on...

But when I attempt to verify and compile, I get this error message for each of the value set lines:

'ADFReg' does not name a type

So what am I doing wrong? I have already tried changing the declaration line by inserting 'int' in between 'unsigned' and 'long', but this had no effect.

Can anyone tell me how to fix this one, please?

Jim Rowe :confused:

Well, you didn't post your code so I'm gonna have to guess. Are you trying to do those assignment statements in global-land rather than inside a function? If so, that's not permitted.

It's difficult to say what you're doing wrong as we have no idea what you're doing. Perhaps the next time you solicit comment you might care to share a glimpse of your code?

In any event, that error message typically pops its head up when you try to initialize a variable outside of a function (except during declaration). Does this look like it might be the case?

you cannot assign a value to variable outside a function.

you may initialize it, however:

unsigned long ADFReg[6] = {0x4580A8, 0x80080C9, ..., 0xDEADFEED};

Thanks for those helpful suggestions, folks. I think you've given me the answer, too.

Both the array declaration and the (initial) value assignment lines were ahead of the setup() and (loop) functions. Presumably I should have done the value assignments in the setup() function...

I'll give it a try straight away. Many thanks!

Jim Rowe

"Variable declaration in C++

The act of reserving the required memory space as per datatype and associating it with a name is variable declaration (int a;). Assignment is storing a value in the location using assignment operator(a=2;). If a value is assigned to the variable at the time of declaration itself, it is called initialization (int c=2;). If a variable is declared without initialization, it will have random garbage value stored in it."

I did try moving the assignment statements down into the setup() function, and that problem was indeed solved. So thanks to all for putting me straight.

Jim Rowe

232:
If a variable is declared without initialization, it will have random garbage value stored in it."

unless it is declared in global space, then it will have a value of zero/null/nullptr.

JimRowe:
I did try moving the assignment statements down into the setup() function, and that problem was indeed solved. So thanks to all for putting me straight.

Jim Rowe

That does not make much sense.
Since the terminology got little mixed up - I like to see the whole code , but since I have no access to verify it - just "let it go " for now.
I have a gut feeling the issues is using an array , which should be somewhere in very basic 'C/C++" documentation if I am right.