Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Wednesday, March 4, 2020

Pointer in C

Pointer: is a variable that stores the address of another variable. Like integer pointer stores the address of int variables, and so on.

Pointer in C | welcome2protec.com

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
C pointer | welcome2protec.com
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 

--



Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us