64-bitįor 64-bit int variable, it will be incremented by 4 bytes. 32-bitįor 32-bit int variable, it will be incremented by 2 bytes. Where i is the number by which the pointer get increased. new_address= current_address + i * size_of(data type).The Rule to increment the pointer is given below: We can traverse an array by using the increment operation on a pointer which will keep pointing to every element of the array, perform some operation on that, and update itself in a loop. This is somewhat different from the general arithmetic since the value of the pointer will get increased by the size of the data type to which the pointer is pointing. If we increment a pointer by 1, the pointer will start pointing to the immediate next location. Following arithmetic operations are possible on the pointer in C language: In pointer-from-pointer subtraction, the result will be an integer value. However, as we know that pointer contains the address, the result of an arithmetic operation performed on the pointer will also be a pointer if the other operand is of type integer. We can perform arithmetic operations on the pointers like addition, subtraction, etc. OutputValue of x: 10 Pointer Arithmetic in C The NULL pointer is a constant with a value of zero defined in several standard libraries. A pointer that is assigned NULL is called a null pointer. This is done at the time of variable declaration. It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done by using unary operator * that returns the value of the variable located at the address specified by its operand. (a) We define a pointer variable, (b) assign the address of a variable to a pointer and (c) finally access the value at the address available in the pointer variable. There are a few important operations, which we will do with the help of pointers very frequently. The only difference between pointers of different data types is the data type of the variable or constant that the pointer points to. The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the same, a long hexadecimal number that represents a memory address. Take a look at some of the valid pointer declarations −int *ip /* pointer to an integer */ However, in this statement the asterisk is being used to designate a variable as a pointer. The asterisk * used to declare a pointer is the same asterisk used for multiplication. Here, type is the pointer’s base type it must be a valid C data type and var-name is the name of the pointer variable. The general form of a pointer variable declaration is −type *var-name Like any variable or constant, you must declare a pointer before using it to store any variable address. A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location.
0 Comments
Leave a Reply. |