A pointer is a variable that stores the address of another variable of same data types. Like integer pointer stores the addresses of int variables, same way char pointer stores the addresses of char variables and so on. A Pointer in C is used to allocate memory dynamically i.e. at run time. The pointer variable might be any of the data type such as int, float, char, double, short and so on.
Declaration of pointer variable:
Just like any other variables in C, it must be declared first before using it. The general form of declaring a pointer is-
/*Syntax to declare a pointer:*/
Data_type *Variable_name;
For Example: Declaration of pointer variables
int *ptr; // ptr is a pointer of int type
char *c; // c is a pointer of char type
float *z; // z is a pointer of float type
double *y; // y is a pointer of double type
Remember: The data type of pointer and variable must be same, since an int pointer can hold the address of int variable, same way pointer declared of char data type can hold the address of char variable.
Note: Asterisk (*) symbol is used to indicate that *ptr is a pointer variable not a ordinary variable.
How to store address of a variable to a pointer variable?
Since we know that, the address of a variable can be obtained using the ampersand sign (&) also known as address of operator. So, the address of a variable will be assigned to a pointer variable in the following manner. See below diagram to understand it better.
/*Ordinary variable*/
int var=10;
/*declaration of pointer, used to hold the address of another variable*/
int *ptr;
/*This is how you can store address of var to a pointer variable *ptr*/
ptr= &var
Memory representation of pointer variable | welcome2protec.com |
Note: In previous tutorial we already learned about, what is address or memory location and how to access that location using address of operator (&). If you've any doubt so please visit to learn it first-
How to access value of var using pointer?
To display the value of var using pointer, the unary operator * is used which returns the value of variable var whose address is stored by the pointer (i.e. *ptr). Let's have a look at the below example.
int var=10;
int *ptr;
ptr= &var
/* We are using *ptr to access the value stored at the memory location
* which ptr is currently pointing to. */
printf("%d",*ptr);
Output: 10 // will be displayed on the screen
Similarly if you increment the value of var then the output will be changed. See the below example
int var=10;
int *ptr;
ptr= &var
var++;
printf("%d",*ptr);
The output will be 11 not 10. Since the value of var is incremented by 1.
Example 2: How to Use Pointers?
There are a few important operations, which we will do with the help of pointers very frequently. (a) We define a pointer variable, (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. The following example makes use of these operations-
#include
int main(){
int var = 20; /* actual variable declaration */
int *ip; /* pointer variable declaration */
ip = &var; /* store address of var in pointer variable*/
printf("Address of var variable: %x\n", &var );
/* address stored in pointer variable */
printf("Address stored in ip variable: %x\n", ip );
/* access the value using the pointer */
printf("Value of *ip variable: %d\n", *ip );
return 0;
}
Output:
When the above code is compiled and executed, it produces the following result −
Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20
--
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.