What is pointer arithmetic




















The address is the memory location that is assigned to the variable. Hence, there are only a few operations that are allowed to perform on Pointers in C language. The operations are slightly different from the ones that we generally use for mathematical calculations. The operations are: We provide nothing but the best curated videos and practice problems for our students.

Check out the C Foundation Course and master the C language from basic to advanced level. Wait no more, start learning today! When a pointer is incremented, it actually increments by the number equal to the size of the data type for which it is a pointer. For Example: If an integer pointer that stores address is incremented, then it will increment by 2 size of an int and the new address it will points to While if a float type pointer is incremented then it will increment by 4 size of a float and the new address will be Decrement: It is a condition that also comes under subtraction.

When a pointer is decremented, it actually decrements by the number equal to the size of the data type for which it is a pointer. For Example: If an integer pointer that stores address is decremented, then it will decrement by 2 size of an int and the new address it will points to Note: If you still have any confusion, you can always use around expression which you want to evaluate first. Two pointers are equal when they both are null, or contains the address of the same variable.

Use of these i. As we know the pointer is a variable that contains the memory address. The pointer variable itself occupies some space in memory and hence it also has a memory address.

We can store the address of the pointer variable in some other variable, which is known as pointer to pointer. The syntax of declaring a pointer to a pointer is as follows:. On dereferencing the address stored at ip we will get the value stored in the variable i. Load Comments. Toggle navigation Home. Blog Contact About. In this chapter, we will discuss arithmetic operations that can be performed on pointers.

When the integer value is converted to an address offset, the compiler assumes that only memory positions of the same size lie between the original address and the address plus the offset. This assumption is valid for array members. By definition, an array is a series of values of the same type; its elements reside in contiguous memory locations. However, storage for any types except array elements is not guaranteed to be filled by the same type of identifiers.

That is, blanks can appear between memory positions, even positions of the same type. Therefore, the results of adding to or subtracting from the addresses of any values but array elements are undefined. Similarly, when two pointer values are subtracted, the conversion assumes that only values of the same type, with no blanks, lie between the addresses given by the operands.



0コメント

  • 1000 / 1000