Thursday 19 February 2015

Using Pointers In Arduino

what is a pointer ?

a pointer is a variable that holds the location of literally the address of a variable stored in computer memory


so pointer is basically a variable with special powers... different from normal variables which can store values, pointers are special variables that can hold the address of variable

when we declare a variable.. Three things happen

1.Computer memory is set aside for the variable
2.The variable name is linked to that location in memory
3.the value of variable is placed into memory

Normally we can access the address of variable in computer memory using the "&" operator.
ok let's declare a variable and check its address using the following code.
small c code (i don't know why "&" is not working in Arduino IDE so gone for the c code )

#include
void main(){
int a = 20;
printf("the value of a is %d",a);
printf("the address where a is stored in memory is %d",&a)
}


i don't have a c compiler in my pc. so i go to the online compiler and the result is
Address of a Variable
the variable holds the value it is assigned.
and the pointer holds the address of the variable.


this little code will gives us the more explanation
#include
void main(){
int a = 20;
printf("the value of a is %d",a);
printf("\nthe address where a is stored in memory is %d",&a);
int* b = &a;
printf("\n the value of the pointer is %d",b);
printf("\n the address of pointer is %d",&b);
}


and the result is
Pointer address

In pictorial Representation
Pointer Pictorial Representation

Pointer Declaration
how can we tell the compiler that we are using a pointer in Program
syntax for Pointer declaration
pointerType *pointerName

pay full attention to that asterisk (*) operator. without that we are declaring a normal variable not a pointer.


* (asterisk operator) it is useful when declaring the pointer and dereferencing it.


pointerType : It specifies the type of pointer. type of the address whose address a pointer can store.
pointer data type allocates an area in memory large enough to hold the machine address of variable.If the address of memory location in typical micro controller is 16 bits, the pointer to a character will be 16 bit value Even though the character itself is only a 8 bit value.

Once the pointer is declared we are now dealing with Address of the variable it is pointing to, Not the value of the variable itself.


pointerName: User specified Name. it is easy to read if we end the name with _ptr

we can use pointers for two purposes
1.For Accessing the address of variable
2.For accessing the value of the variable whose memory address the pointer stores.
#include
main(){
//initialize the pointer
int a = 20;//initialize the integer whose address has to be stored in the pointer
int *a_ptr = &a;//assign the address of variable a to the pointer variable a_ptr
printf("the address stored in a_ptr is %d",a_ptr);
printf("\nthe value at which address is stored in pointer is %d",*a_ptr)
}


Re

In title i say Arduino But all the codes used in this post is using C. I hope This little Arduino Code will make sense after knowing little about Pointers.

void setup(){
Serial.begin(9600);// start the serial Communication
int a = 20; // initalize the Variable a
int *a_ptr = &a; // Initialize the a_ptr and assign the address of variable a to it
int b = *a_ptr;// assign the value at which the address is stores in pointer to the variable b
Serial.print("The Value of a is\t");
Serial.print(a);
Serial.println("");
Serial.print("The Value of b is \t");
Serial.println(b);
Serial.println("");
}
void loop(){
//do nothing here
}

Arduino code

No comments:

Post a Comment