Pointers
- To know where exactly the value of a variable is stored in the memory, you need to be aware of the concept called pointers.
- A pointer is a powerful construct of the C programming language.
- A pointer is a variable which holds the address of other variables such as arrays, structures and functions that are used in a program.
- It contains only the memory location of the variable rather than its content.
Advantages
- To point to different data structures.
- Manipulation of data at different memory locations is easier.
- To achieve clarity and simplicity.
- More compact and efficient coding.
- To return multiple values via functions.
- Dynamic Memory Allocation.
Operations used with pointers
- The address operator – &(ampersand)
- The indirection operator – *(asterix)
- An address operator gives the address of a variable(not constants or expression); while the indirection operator gives the value of the variable that the pointer is pointing to.
- Both the pointer variable and the variable to which pointer points to, must be declared.
- Also, a pointer variable must be assigned the address of the declared variable before it is used in the program.
Pointer Dereferencing
- This is a process of referencing the contents of the variables using pointers, indirectly.
- Steps involved - Address of a variable, whose value is to be referenced is assigned to a pointer. Then, this pointer points to the value.
Operations on Pointers
- A pointer variable holds the address of the other variables. This address can be incremented or decremented.
- However, the pointer variables cannot be multiplied or divided.
- A pointer variable can be assigned the address of other variable.
- If two pointer variables are pointing to the object of the same data type, then they can be assigned.
- A pointer variable can be assigned a NULL value(NULL = 0).
- An integer value can be added to and subtracted from a pointer variable.
- One pointer variable can be subtracted from another pointer variable, if both are pointing to the elements of the same array.
- If two pointer variables are pointing to the objects of the same data type, then they can be compared with one another.
Pointer Initialization
- As ordinary variables are initialized within the declaration part of a program, the pointer variables can also be initialized by assigning the addresses of another variables that are used in the program.
- data_type *ptr_var = expression;
- A pointer can also be initialized by assigning a NULL value.
Pointers and Functions
- Like other parameters, pointers can also be passed to functions.
- This is carried out by the call-by-reference parameter passing mechanism.
Call-by-Reference
- This is a mechanism by which the pointers can be passed as arguements to the functions.
- Thus, the data items of the calling program can be assessed by the called program(function).
- No value is copied when the pointers are passed as arguements, as in the call-by-value method.
- Another important point is that if the values are changed in the functions, this will modify the original contents of the original parameters.
- In the calling program, the function is invoked with the function name and addresses of the actual parameters enclosed within the parantheses.
- function_name(&var1,&var2,…&varn);
- In the parameter list of the called program each and every formal parameter(pointers) must be preceded by an indirection operator(*).
- data_type function_name(*var1,*var2,…*varn);
Pointers and Arrays
- Pointers can be used with arrays for efficient programming.
- One-dimensional Arrays
- The name of an array itself designates some memory location and that location in main memory is the address of the very first element of the array.
- &aarray[0] or simply array.
- The addressof the i-th element of the array - &array[i-1] or (array+(i-i))
Dynamic Memory Allocation
- This is a process of dynamically allocating memory to any variable that is being used in your C program.
- When the program is terminated, the dynamically allocated memory is deallocated.
- malloc(), calloc(), free().
- malloc() – This function is used to allocate the memory in bytes.
- malloc(nob);
- free() – This function frees a block of allocated memroy.
- free(p);
Related posts:








Leave your response!