Pages: [1]   Go Down
Author Topic: reference and deference help  (Read 709 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 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
Logged

Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-lol
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
God Member
*****
Karma: 1
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

&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().
Logged

Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I actually figured it out waiting for a reply, thanks tho here's what I did
Code:
Shift(&smiley[0]);

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?
« Last Edit: February 04, 2012, 10:49:50 pm by winner10920 » Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

http://pw1.netcom.com/~tjensen/ptr/pointers.htm
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Offline Offline
God Member
*****
Karma: 1
Posts: 547
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thanks for the reference material James
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

Code:
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 () {}
Logged


Offline Offline
Edison Member
*
Karma: 5
Posts: 1730
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 485
Posts: 18806
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 302
Posts: 26328
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Pages: [1]   Go Up
Jump to: