Pointers Definition | Declaration and Initialization in c programming msbte

Pointers Definition | Declaration and Initialization in c programming msbte

Pointers Definition | Declaration and Initialization in c programming msbte
Pointers Definition | Declaration and Initialization in c programming msbte

Definition Of Pointer

  • Pointer is a variable which stores memory address of other variable.
  • Pointer in C is a user defined data type. It is build by using primary data types. Variables hold values and pointers hold their memory addresses where these variables are located in memory.
  • A pointer variable is therefore nothing but, a variable that holds address of another variable in memory.Memory address is a positive number so its type is unsigned integer. Since pointer is a variable it is also stored at another location in memory and the values stored in it are of unsigned integer type.
  • The values whose addresses are stored in a pointer variable can be of type char, int, float, double, etc.Let's see how to declare the variables and initialize address of variable to it.

Declaration Of Pointer

  • In the program pointers are declared at same location where we declare variables i.e. in declaration section.Declaring a pointer is somewhat similar to declaring a variable.
  • To indicate which type of value is stored in a variable we use data type while declaring a variable. e.g. to hold integer type of value the data type of variable should be integer, to hold a character the data type of variable should be char and to hold real number the data type of variable should be float or double.
  • Similarly while declaring a pointer; we should know the data type of the value to which it is going to point, so that we can declare a pointer of that data type. This means suppose a pointer stores address of a variable of type integer then we declare pointer as integer.
  • Syntax
  • The syntax of pointer declaration is as follows
  •    data_type *pointer_name;
  • where specifies that it is pointer variable.

  • Example of integer

      int *ptr;
  • Here int is the data_type of pointer and ptr is the name of pointer. While declaring the pointer, same guidelines and rules should be followed for pointer_name as given for variable_name in variable declaration.
  • The above example tells that the pointer ptr is of type integer which means it stores the address of an integer variable in memory

  • Example of character

  • Another examples of pointers are as follow.
  •   char *cptr;
      float *fptr;
  • Here char *cptr specifies that cptr points to character type of value. This cptr pointer will be stored at a specific memory location.
  •   float *fptr;
  • will going to point a float value and this fptr pointer will be stored at a specific memory location.

Initialization Of Pointer

  • After declaring the pointer, the next step is to assign the address of certain variable to it .This is called as pointer initialization. It connects variables and pointers.
  • Syntax
  • Syntax for initializing a pointer.
  •   pointer_name = &variable_name;
  • Variable_name is the name of that variable to which the pointer pointer_name will point.
  • Example
  • Suppose variable a is an integer. Then initializing the address of a to a pointer can be done in the following way:
  •   int *ptr; 
      int a; 
      ptr = &a;
  • In above statements, the first line we have declared pointer ptr of integer type.
  • In second line we have declared an integer variable a.
  • And in the third line, the address of a is assigned to the pointer ptr. That's why now the pointer ptr is pointing to the variable a as shown in Fig Below
Fig 1
  • Suppose the variable a stores at location 2000 and the pointer variable ptr is located at location 5000.
  • Here ptr=&a; is used to give the address of a to ptr variable so in the memory at location 5000 ,the 2000 is copied.
  • The dashed line says that the ptr points the value located at location 2000.
  • As variable declaration and initialization can be combined in single statement, in the same way the declaration of pointer and initialization of pointer can be combined in one statement as follows:
  • int *b = &a;
  • Consider a is an integer which holds an integer value 5.  So above statement states that the  pointer variable b variable holds the address of an integer variable a.
  • Another examples of pointers are:
  • char ch='A';
    char *q;
    q=&ch;
  • Here the first statement initializes the character A to variable ch. In second statement char *q means q is a pointer that will point character value stored in variable ch. It can be represented in Fig. Below

Fig 2




Post a Comment

Previous Post Next Post