C : POINTERS


POINTERS



INTRODUCTION :


  1. Pointer variables are the special types of variables that hold memory address rather than data, i.e. a variable that holds address value of another variable is called a pointer variable or simply a pointer.

  2. A pointer can also be defined as a variable whose value is the address of another variable, i.e. direct address of the memory location.

  3. Pointer is a variable that stores the address of another variable. They can make some things much easier, help improve your program’s efficiency, and even allow you to handle unlimited amounts of data.

  4. C pointer is used to allocate memory dynamically i.e. at run time. The variable might be any of the data type such as int, float, char, double, short etc.

  5. SYNTAX :
    
    data_type *ptr_name;
    
    


DECLARATION OF POINTER :


  1. Pointer declaration is similar to other type of variable except asterisk (*) character before pointer variable name.

  2. The data type that appears in the declaration refers to the object of the pointer, i.e., the data item that is stored in the address representedby the pointer, rather than the pointer itself.

  3. Within a variable declaration, a pointer variable can be initialized by assigning it the address of another variable.

  4. Remember that the variable whose address is assigned to the pointer variable must have been declared earlier in the program.



POINTER TO FUNCTION :


  1. Pointers are often passed to a function as arguments.

  2. This allows data items within the calling portion of the program to be accessed by the function, altered within the function, and then returned to the calling portion of the program in altered form.

  3.   When pointers are used as arguments to a function, some care is required with the formal argument declarations within the function.

  4. Specifically, formal pointer arguments that must each be preceded by an asterisk. Function prototypes are written in the same manner.

  5. If a function declaration does not include variable names, the data type of each pointer argument must be followed by an asterisk.


POINTER ARITHMETIC :


  1. A pointer variable can be assigned the address of an ordinary variable (e.g., pv = &v).

  2. A pointer variable can be assigned the value of another pointer variable (e.g., pv = px) provided both pointers point to objects of the same data type .

  3. A pointer variable can be assigned a null (zero) value (e.g., pv = NULL, where NULL is a symbolic constant that represents the value 0).

  4. An integer quantity can be added to or subtracted fkom a pointer variable (e.g., pv + 3, ++pv, etc.)

  5. One pointer variable can be subtracted fiom another provided both pointers point to elements of the same array.

  6. Two pointer variables can be compared provided both pointers point to objects of the same data type.

  7. Thus, a pointer variable cannot be multiplied by a constant; two pointer variables cannot be added; and so on.

  8. Also, an ordinary variable cannot be assigned an arbitrary address (i.e., an expression such as &x cannot appear on the left side of an assignment statement).


POINTER TO POINTER :


  1. It we want to store the address of the pointer we use the concept of pointer to pointer.

  2. Storing the address of a pointer variable is also known as DOUBLE POINTER.

  3. SYNTAX : datatype **ptr_name


Comments