How do Arrays work?

Say you have an array right, now if I’m correct based on my knowledge, it’s like

int A[] = [1, 2, 3];

Now if this is somewhat correct, how could one print to say, variable B as the array numbers in series.
For this specific example, if you print out B every time it prints a number in the array, it goes on to the next one.
So the serial.println would be

B=1
...
B=2
...
B=3
...

Now how would the code look based in these examples?

int B;
for (int x = 0; x < 3; x++)
{ 
  B = A[x];
  Serial.print("B=");
  Serial.println(A[x]);
}
A[] = [1 2 3];

The syntax for declaring and initializing an array is

int A[] = {1,2,3};

To access the individual elements (pieces) of the array ( the elements are numbered 0,1,2):

Serial.println(A[0]);
// or
int B = A[1];
// or if B is already declared elsewhere
B = A[2];

To print out the contents of the A array in order

for(int n = 0; n < sizeof(A)/sizeof(A[0]; n++)
{
   Serial.println(A[n]);
}

@OP

Two Questions:

1. What is the definition of an array in C/C++ Language?

2. What is the definition of 'definition'?

GolamMostafa:
2. What is the definition of 'definition'?

How does that help the OP ?

...R

An array is a set of numbers in a series that helps to compress code and access it easier

How do you add to the array, say if you wanted to add a 4 to the end of 3 in the array of A?

Robin2:
How does that help the OP ?

It will help me to help OP to declare an array in the way it should be. Let us go to the Bar Sport to discuss it – What is the definition of ‘definition’.

depression:
An array is a set of numbers in a series that helps to compress code and access it easier

How do you add to the array, say if you wanted to add a 4 to the end of 3 in the array of A?

This is a set of numbers: 12, 23.5, 0x45, 0.05, 452; are they making an array?

depression:
How do you add to the array, say if you wanted to add a 4 to the end of 3 in the array of A?

You can't. If you want to be able to add yonit later then you have to make the array large enough to start with. If you want a larger array you'd have to delete the one you have and create one larger. But that gets into dynamic allocation which is a bit advanced when dealing with microcontrollers. If you don't understand it then you shouldn't mess with dynamic allocation. If you do understand it then you know why it should be avoided.

More homework questions...

depression:
How do you add to the array, say if you wanted to add a 4 to the end of 3 in the array of A?

A[2] += 4;

Hmm, so can you CHAnGE A to make a different array?
Like

int A[] = {1, 2, 3};
random code stuff
int A[] = {1, 2, 3, 4};

Would this work

An array is a specific series of numbers in order that can help to compress the code

Would this work

What happened when you tried it?

I tried your code AWOL and it worked. Thanks famine

An array is a specific series of numbers in order that can help to compress the code

It does not have to be numbers as long as all of the elements are of the same data type.

You can have
an array of characters for example.
char characters[] = {h,e,l,l,o};
It has been, rightly pointed out by GolamMostafa, that that code is not correct.

char characters[] = {'h','e','l','l','o'};

is the right syntax.

Or an array of servo objects.

Servo servoArray[3];

I tried your code AWOL and it worked. Thanks famine

Post the code that “worked”.

@OP

Have you noticed that the following code(s) is not compiled? Can you correct it?

groundFungus:
char characters = {h,e,l,l,0};

Right, brain fart on my part.

This is an error and not an ignorance. Many veterans of this Forum are reluctant to accept it; sometimes, they claim that a poster should act like a programmed machine which does not do mistake.

groundFungus:
Right, brain fart on my part.

I expect that you meant

char h = 'h';
char e = 'e';
char l = 'l';
char o = 'o';
char characters[] = {h, e, l, l, o};

Which works :slight_smile: