View Single Post
  #7   Spotlight this post!  
Unread 13-12-2007, 01:51
Tom Bottiglieri Tom Bottiglieri is offline
Registered User
FRC #0254 (The Cheesy Poofs)
Team Role: Engineer
 
Join Date: Jan 2004
Rookie Year: 2003
Location: San Francisco, CA
Posts: 3,187
Tom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond reputeTom Bottiglieri has a reputation beyond repute
Re: How to manipulate a variable value using an array?

Quote:
Originally Posted by amateurrobotguy View Post
Why bother with the int *ptr declaration when it has to be used again in the b= part? This is the example from Wiki.
Code:
int *ptr; //ptr is a pointer to the address an 'int' somewhere in memory
int a, b; //compiler chooses where these go in memory, each have an address

a = 10; //set value of a to 10
//pass a by reference to ptr
ptr = &a; //ptr now holds the address of where a is in memory
b = *ptr; //b now holds the value (use *) that ptr points to
In this case, b would be equal to a at the end.
The first line declares a pointer, which is different from a regular variable.
You can do other cool things with this, too.

Code:
void idouble(int * input){
   *input *= 2 ;
}

void main(){
    int x ;
    x = 5 ;
    idouble(&x) ;
    print(x);
}
This will print 10.

Last edited by Tom Bottiglieri : 13-12-2007 at 01:58.