Trying hard to get the darn pointers to work!

I have made lots of lights blink; figured out a circuit that would allow me to rectify and chop down 24VAC to 5VDC so that I could monitor it and lots of other fun things. Now I'm ready to start writing some libraries so that I can reuse some of the code. I wanted to use good programming techniques and not expose all my variables to everything else, so I was trying to learn pointers to pass arrays. I've hit a road block and hope I can get some help.

I've read quite a bit now on pointers and decided to take one or two for a spin. I used some code that I found pretty much verbatim off of this forum (and other reference documents) and am still having compiling errors (I may have changed the names to protect the guilty). From all the stuff that I've read this should work no problem.

int i = 5;     // Just an int named i that has the value 5
int j = 6;     // Just an int named j that has the value 6
int *p1 = &i;  // Just a pointer p1 that points to an int and that int is named i
int *p2;       // Just a pointer p2 that points
p2 = &j;       // Make that pointer p2 point to that int name j


void setup() 
{
  Serial.begin(9600);
}

void loop() 
{
  delay(3000);
  Serial.println(*p1);  //Print the variable where p1 points and hope it's 5
  Serial.println(*p2);  //Print the variable where p2 points and hope it's 6
  Serial.println("YEA");  //Celebrate!!!!
}

I get the following error when compiling:

arduino-builder/arduino-builder -compile -core-api-version 10611 -build-path /tmp/945288450/build -hardware arduino-builder/hardware -hardware arduino-builder/packages/cores -tools arduino-builder/tools -tools arduino-builder/packages/tools -built-in-libraries arduino-builder/latest -libraries /tmp/945288450/pinned -libraries /tmp/945288450/custom -fqbn arduino:avr:uno -build-cache /tmp -logger humantags -verbose=false -logger humantags /tmp/945288450/Test_Passing_Arrays_to_Functions
/tmp/945288450/Test_Passing_Arrays_to_Functions/Test_Passing_Arrays_to_Functions.ino:5:1: error: 'p2' does not name a type
exit status 1

Why in the world does that compiler not like p2 being initialized to &j when it was happy to to make p1 point to i?? I am using the web DE.

Move the assignment code

p2 = &j;

to the setup routine and the code will compile.

This is a problem specific to the Arduino build system as the IDE mangles the code before it's presented to the compiler. For this it expects the code to follow the rules and the rules say that only declarations may be outside the setup routine all other code must be in setup.

pylon:
This is a problem specific to the Arduino build system as the IDE mangles the code before it's presented to the compiler. For this it expects the code to follow the rules and the rules say that only declarations may be outside the setup routine all other code must be in setup.

You are wrong.

It is a standard C(++) 'limitation'.

OP:
The problem is you’re handling p2 in two steps:

int *p2;
p2 = &j;

This is a variable definition followed by an assignment statement. But, an assignment statement is executable code. You can’t have executable code hanging out in Global Land. It needs to be in a function.

Note the difference with how you handle p1:

int *p1 = &i;

This is a combined variable definition and initialization (not an executable assignment). Initializations happen at compile-time, not run-time.

So, make p2 an initialization and get rid of the assignment statement.

int *p2 = &j;
//p2 = &j;