reference and deference help

Hey I’ve never used reference or deference before and I’ve read a bit and it just boggles my mind unfortunately
I want a function to read an outside array and to return the modified array
im pretty sure the way to do this is to have the function first be pointed to the array, then modify it, I don’t exactly know how this looks like since what I’ve read doesn’t really show that and I can’t put 2 and 2 together to see it, atleast for now
But I also can’t figure out how to return an entire new array or replace the original one with the modified

I didn't try it but here's what I have for say a single int

Shift(&number); //give deference or location of number

Shift(int address){ Int variable = *address; //stores the value of number to variable //do work on variable &address = variable; //puts variable back in place of number so when number is used it has the modification }

Is that correct? And can I use it to modify an entire array? Is there a way to just skip all this and return an array?

Or maybe someone know an easier way of what im trying to do, im trying to make functions to flip,shift,or rotate an 8x8 array for a matrix, im trying to do it myself but if someone knows a shortcut I wont mind XD

Instead of returning an array, make an argument of your function a pointer to the new array.

Think of how sprintf() works. You have to give it a buffer in the function call, it doesn’t “return” anything.

&variableName is an object REFERENCE to the Class that variableName is an instance of. Members are then accessed via dot syntax ex. variableName.function(). Whereas *variableName is a pointer to the location where variableName's value resides(memory location), if variableName is a class then member functions are accessed like this variableName->function().

I actually figured it out waiting for a reply, thanks tho here’s what I did


Void shift(int* address){
int endbit;
for(int I; I <8; I++){
Endbit = adress[I] & 1;
address[I] = address[I] >> 1;
address[I] = address[I] | (endbit * 128);

now I still don’t really understand the workings, its just too much to get in my head all at once, kinda like back when I was first learning transistors it took a while until it finally sunk in, I knew what to do just not why
i know this works but do you guys think its a proper solution?

I use a PDF of this reference when working with pointers:

Lol that’s exactly the page I found before in my attempt to figure this out, kinda useful if only I can grasp it and remember it

thanks for the reference material James

You are better off passing by reference. Your code makes it look like you are passing array item 0 (and therefore that is what you are interested in) but you are passing the address of the whole thing. This is neater IMHO:

const int MAX_THINGS = 8;
void rotate (int (& data) [MAX_THINGS])
  int endbit;
  for (int i = 0; i < MAX_THINGS; i++)
    endbit = data [i] & 1;
    data [i] = data [i] >> 1;
    if (endbit)
      data [i] = data [i] | 0x8000;
    }  // end of for
  }  // end of shift

void setup ()
  int smiley [MAX_THINGS] = { 2, 4, 6, 8, 10, 12, 14, 16 };
  rotate (smiley);
}  // end of setup

void loop () {}

Im gonna try that, I tried just passing the name and it was giving me some errors, but then again I tried alot of stuff before I got this to work so it was probably the other stuff, Im making a max7219 library for myself, so I definetly want it to look neat in case I look at it in the future and confuse myself again lol

Also I was wondering, since the reference deference thing is altering the original memory, its not gonna waste too much additional space right?

That's right. Apart from "endbit" and the small amount of stack used for the function call, it's quite efficient.

Think of how sprintf() works. You have to give it a buffer in the function call, it doesn't "return" anything.

Does so - it returns the length of the formatted string.