Pointers
  • Pointer Introduction
  • The Address(&) and Indirection (*) Operator
  • Declaration and Initialization
  • const pointer
  • Void pointer or Generic Pointer
  • Null pointer
  • Pointer to pointer(Double Pointer)
  • Pointer Arithmetic
  • Pointer and Array
  • Using Array name itself as a pointer
  • Passing pointer to a function as a function argument
  • Parameter Passing Techniques Using Pointers as Arguments
    • call by value
    • call by address
  • Function Returning value
    • Returning More than one value From A Function
    • Functions Returning Address
    • Function Returning Pointers
  • Pointer and Structure

Pointers

Well, it is believe that real power of C lies in the proper use of pointers. However it may cause nightmares, if they are not properly handled. Pointers is a type of variable which holds the memory location of other variable. A pointer can be incremented/decremented, i.e, to the next/previous memory location.

Pointer Syntax
Here is how we can declare pointers.
int* p;
int *p;



Advantages of Pointers in C

  1. Pointers are useful for accessing memory locations.
  2. The purpose of pointer is to save memory space and achieve faster execution time.
  3. Pointers are used to return multiple value from function.
  4. Pointers helps in dynamic memory allocation.
  5. Pointers can be used with array and string to access elements more effeciently.

Difference between Array and Pointer

Array Pointers
An array is a variable and cannot be appear on the left hand side of an assignment operator once it is declared. A pointer is a variable which can be appear on the left hand side of an assignment operator after declaration.
E.g. if a is array name then a = &variable; (invalid) E.g. if p is pointer variable then p = &variable; (valid)


Relationship between Array and Pointer

Array is known as constant pointers since name of the array itself represent the memory location of first element of an array. So, array is also called constant pointer.




We can access the values and addresses of an array element by following methods

1. Normal Method in 1D Array




2. By using array name itself as a pointer in 1D Array




3. By using pointer variable in 1D Array




1. Normal Method in 2D Array




2. By using array name itself as a pointer in 2D Array




3. By using pointer variable in 2D Array




Assignment Set:
  1. What is pointer? What are the main reasons of using pointer?
  2. Differentiate between ordinary variables and pointer variables.
  3. Reference operators (&) Dereference operators (*)
    Address of operator ("&") is known as reference operator Value of operator ("*") is known as deference operator or indirection operator.
    This operator returns the address of the variable associated with the operator. This operator returns the value stored in the variable pointed by the specified pointer.
    For e.g., if we write “&x”, it will return the address of the variable “x’. For e.g., if we write “*p”, it will return the value of the variable pointed by the pointer “p”.
    Hence, if we have a pointer “p”, which we want to point to a variable x, then we need to copy the address of the variable “x” in the pointer variable “p”.
    This is implemented by the statement: p = &x;
    Hence, if we want the value of the variable pointed by the pointer “p” to be stored in a variable “y”, then the expression can be written as: y = *p;
Board Exam Theory Questions
  1. The indirection operator is an unary operator represented by the symbol (*). We use the operator (*) in front of a variable in two distinct ways with pointers, for declaration and for dereference.
    When the pointer is declared, the star (*) indicates that it is a pointer, not a normal variable.
    When the pointer is dereferenced (i.e. use of (*) in front of variable except in variable declaration), the indirection operator is used to obtain the value of a variable to which a pointer points.

    For example:
    int x = 5, y;
    int *p;
    p = &x; //Pointer p pointing memory address of x
    y = *p; //indirect access to the value of the variable x

    Since the pointer pointing to a variable provides an indirect access to the value of the variable stored in its memory address, the pointer operator is also called indirection operator.
  2. A pointer variable is declared with specific data type, like any other normal variable, so that it will work only with memory address of a given type. Just as integer variable can hold only integers, a character variable can hold the only characters and so forth, each pointer variable can point only to one specific type (int, char, float, double or any user defined data type).

    For example:
    int x = 5, y;
    int *p;
    p = &x; //Sotring the address of x
    // And while dereferencing a pointer variable i.e.
    y = *p; //dereferenced i.e. y = 5;

    Due to the ability of a pointer to directly refer to the value that it points to, it becomes necessary to specify in its declaration which data type a pointer is going to point.
  3. An expression like Ptr++ will cause the pointer Ptr to point tot he next value of its type. For example, if Ptr is an integer pointer with an initial value 1000, then after the execution of statement Ptr++; the value of Ptr will be 1002, but not 1001. That is, when we increment a pointer, its value is incremented by the size of the data type that it points to. And the size of the data type is called the scale factor. The scale factros of various data types are shown below.

    Data type Scale factor
    char 1
    int 2
    float 4
    double 8

    However, the exact scale factor may vary from one compiler to another or from one machine to another.
  4. Explain how pointers can be used in C. [1] [068]
  5. What type of arithmetic operations can be implemented in pointer? [3] [069]
  6. Write down advantages of pointer. [2] [069]
  7. Write down advantages of pointer. [2] [071]
  8. What are the advantages of using pointer in C programming? [2] [074]
  9. What are the importance of “Pointers” in C-Programming language? [4] [068]
  10. Illustrate with example that “Array is indirectly a pointer”. [4] [2071]
  11. How can you access array elements using pointer. [1] [068]
  12. Briefly explain array of pointers. How are array and pointer related? Give example. [5] [2070]
  13. Compare array and pointer with example. [3] [2070]
  14. Also describe the relationship between array and pointer with appropriate syntax and examples. [3] [069]
  15. Explain the relation of array and pointer. [2] [067]
  16. Explain call by reference. How are pointers used in call by reference? [2] [2069]
  17. What is the role of pointers in passing parameters to functions by reference? [4] [068]
  18. What is meant by call by value and call by reference? [3] [067]
Programming Set
  • Write a program that swaps two variables. Use function and pointers.
  • Solve the matrix multiplication Programming using pointers.

Pointer

1D Array and Pointer
2D Array and Pointer
String and Pointer

Pointer & Function

Passing 1D Array using pointer
Passing 2D Array using pointer
String and Pointer with Function