POINTERS IN C

Posted by Unknown 0 comments


We have discussed about the basics of C.But without the use of pointers it is incomplete.
So what are pointers?Why do we need them? All these questions will be answered in the following
discussion.Pointers are the variables that contain address of another variable.



What are Pointers?

Pointers are the variables but they are different from ordinary variables in way that they are used to store the address the other variables.Also these address is a unsigned number,hence pointers always contain in them the unsigned value.Use of pointers can be seen in command line i/o
which consider *argv (the array of pointer to string).

Declaration of Pointers

Let us understand this with the help of an example.

int *a : a is going to contain the address of int variable.
Here a is a pointer variable which contains the address of a int variable.
Lets take another declarations for understanding the concept more effectively.

char *ch : ch is going to contain the address of char variable.
Now try to figure out the meaning of --> float *s.

Basics of Pointers

  • int i=3
i-->location name
3-->value at location
65524-->location number
This declarations tells the compiler to :
1.Reserve space in memory to hold the integer value.
2.Associate name “i” with this memory location.
3.Store value 3 at this location.
  • int *j;   // it tells compiler that j is pointer variable which will going to store address of int variable.
  • j=&i;   // it specifies that i’s address will get stored in j.

j->location name
65524-->value at location
65522-->location number


 Check out the File handling post to get more clear about the pointers concept


Operators used in Pointers

1.Value at address:Denoted by *(asterisk )
2.Address of :Denoted by &(ampersand)



Format Specifier used in Pointers

%u is the format specifier used print the unsigned integers.

Example:

   1:  #include<stdio.h>
   2:  #include<conio.h>
   3:  void main()
   4:  {
   5:   int i=3;
   6:   int *j;
   7:   
   8:   j=&i;
   9:   
  10:  printf("%d",i);//3
  11:  printf("%u",&i);//65524
  12:  printf("%u",j);//65524
  13:  printf("%u",&j);//65522
  14:  printf("%d",*(&i));//3
  15:  printf("%d",*j);//3
  16:  }