Arrays, pointers, classes, and structs

Hello again everybody!

I am just about to run out the door for work, but I wanted to "poll the audience" real quick.

In a previous thread, my eyes were opened to arrays and what they might be able to accomplish for my particular project. I read up on them as best I could, saw a couple examples, and made a few simple sketches of my own to play around with how they work. I still don't think I quite grasp it, but I am getting there at least.

A couple of folks (Johncc and Cr0sh, to name two) instructed me to take a deeper look into arrays as they pertain to arrays of structs and classes, and using arrays and pointers together. You would think there would be a million great tutorials about arrays of classes, arrays of structs, and arrays using pointers out there- but I just can't seem to find any!

Anyone have any useful links or knowledge of where I can learn about this stuff? I read everything (I think I have anyway!) about it I could find on the "playground" and "learning" areas of already... I couldn't find anything that discussed what I am specifically looking for. Google didn't seem to be much help either- perhaps I am searching with the wrong verbage...

Any help you can provide will be greatly appreciated!

An array is just a block of memory, nothing more, nothing less. It has an associated element size to it (1 for a byte array, 2 for an int array, 4 for a long array, etc), which is used to calculate the offset in bytes from the start of the array according to the value in square brackets (simply the element size * the element number).

A pointer is just a variable that points at a location in memory. Again, it can have a size associated with it (1 for a char *, 2 for an int *, 4 for a long *, etc).

A pointer can point at the address where an array is. It can also point to any address within that array. It can even point to somewhere completely unrelated, so use with caution ;)

To access the memory pointed to by a pointer you prepend a *. To get the address of an array element, you prepend an &.

For example:

int vals[20];
int *valptr;

valptr = &vals[3];
*valptr = 1948;
// vals[3] will contain 1948.

If you add to or subtract from a pointer it adds or subtracts in multiples of its associated base size, so an int pointer which you add 1 to will actually add 2 to the address, moving to the next int sized element. Add to the previous bit of code:

*valptr = 7654;
// vals[4] will contain 7654

A struct is just another variable, albeit a complex compound one. Take the following simple structure:

struct coord {
  int x;
  int y;

You can create an array of those simply by using it in place of a normal variable:

struct coord square[4];

Accessing the sub-variables of a struct is done using the . operator:

square[0].x = 3;
square[0].y = 3;

Assigning a pointer is just the same as the int * example above:

struct coord *point;
point = &square[1];

Accessing the sub-variables, though, is now different. Instead of using * and . you use -> Confusing? Yep.

point->x = 7;
point->y = 3;

And you can add/subtract to the pointer as you can with an int...

point->x = 7;
point->y = 7;
point->x = 3;
point->y = 7;

So now the square array contains:

{{3,3}, {3,7}, {7,7}, {3,3}}

Any questions?

Hi Jim,

Google “c++ tutorial” and you can find some more stuff, e.g.

You’ll see a few differences because Arduino is “special”, like

cout << “Hello”; → Serial.print(“Hello”);

std::string s; → String s;

main() → setup() and loop()

But the fundamentals like variables, functions, structures, classes, pointers, and references, are the same.