C Control StatementsC FunctionsC ArrayC PointerC Dynamic MemoryC StringC MathC StructureC UnionC File HandlingC PreprocessorC Command LineC Misc |
pointer arithmetic in c
There are all types of operations not possible with pointers, only some valid operations are valid with pointer, There are some operations are used with pointer OR
(1) Addition of an integer to a pointer and increment operation Increment pointer in C If we increment a pointer by one, the pointer will pointing to the immediate next location. This is the different from the general arithmetic so the value of the pointer get increased by the same size of the data type to which the pointer is pointing. We can traverse an array by using the increment operation on pointer which will pointing to every element of array, perform some operation on that, and update. The Formula for increment pointer new_address= current_address + i * size_of(data type) Here in the formula i is the number by which the pointer get increased. For 64-bit platform int variable, it incremented by 4 bytes For 32-bit platform int variable, it incremented by 2 bytes.
In this example here variable ptr is a pointer to pointer variable that can store the address of pointer variable of data_type. Here the double esterisk used in the declaration indicated or inform to the compiler that pointer to pointer variable is declared. so now take the example with it
In this example first declare variable a and in the second statement pointer variable is declare of type int ptr and stored the address of variable a to the ptr pointer variable, In the last statement pointer to pointer variable is declare pptr which store address of pointer variable
Decrementing Pointer in C Like same as above example of increment, we can decrement a pointer variable. when we decrement a pointer, it start pointing to the previous location. The formula of decrementing the pointer is given below: new_address= current_address – i * size_of(data type)
|