“Pointers
are the fundamental part of C language. In embedded system programming pointer
plays a major role in embedded system programming. For example dynamic memory
allocation is possible only by using of pointers. So it is necessary to understand pointers
base and use it in embedded system programming”
Every
variable that we declare will save on some memory location and its address is
defined. This address of variable can be accessed using ampersand (&)
operator. This operator will give memory address of variable. For example:
Address1=
&Value1; // this statement
assign the address of //value1 to Address1.
Here
Adress1 will contain the address of Value1. Here 30 is the I value of Varaible1
and 4005 will be L value of variable.
So
pointer is variable which holds the memory address of another variable. Pointers
are used to access the memories and manipulate the address. Pointers play a
main role in C programming. I will gives you access the memory location and
manipulate them. If you want to pass a huge amount of data in to a function, it is easier to pass its address location to function then to copy every element of the data. Moreover, if you need more memory for your program then you can allocate more memory using dynamic memory allocation.
Syntax
of pointer declaration is as follows:
Data_type *pointer_name;
Here
Data_type is the type of variable for which it will be to be used. It must be
as valid C data type.
“*” called as dereference operator, this will gives you value at address.
pointer_name
will be name of the pointer.
Why data type of Pointer is required?
As
we studied pointer is used to access variable using its address i.e. pointer
will store the address of variables. We declare pointer according to variables
data types. If we declare a pointer to integer variable data type of pointer
will be integer. The size of integer is
2 byte. It will take 16 bit to store integer value. So pointer data type of
integer will be an integer. So if we access integer array of data using
pointers then to access next value in any pointer increment will be of two
memory location for 8-bit controllers. Same will follow for char (8-Byte) and
float (4-Byte). Example of pointer declarations:
int
variable1=0; //
definition of variable1 as integer //type.
int *pointer1=&variable1; // integer pointer declaration for //variable1 and copies the address of //variable1 in poiter1
char
variable2=0; //
definition of variable2 as char type.
int
*pointer2; //
character pointer declaration for
variable1
float
variable3=0; //
definition of variable3 as float type.
int
*pointer3; //
character pointer declaration for
variable
Accessing data using pointer
There are mainly three steps to use pointers:
- Declaration of pointer variable
- Assigning the address of variable to pointer variable
- Accessing the value at address stored in pointer variable.
For example:
int data1=4, data2; //
definition of data1 ad data2
int *pointer_to_data1; // pointer variable definition // for data1
pointer_to_data1 = &data1; //assigning data1 address to //poiter_to_data1
data2=*poiter_to_data1; // this will copies the
data1 value in //data1, data2 will //contain 4
No comments:
Post a Comment