Latest web development tutorials

C pointer arithmetic

C Pointer C Pointer

C pointer is the address of a numerical representation. Therefore, you can perform arithmetic operations on pointers. Pointer can be four-arithmetic operation: +, -, +, -.

Supposeptr is a pointer to an integer pointer address 1000, is a 32-bit integer, let the pointer arithmetic to perform the following:

ptr++

After performing the above operation,ptr points to position 1004, because ptr each additional time it will point to the next integer position that the current position moved back four bytes.In the case of this operation will not affect the actual value of the memory location, move the pointer to the next memory location. Ifptr points to an address of 1000 characters, the above operation will cause a pointer to the position of 1001, because the next character position in 1001.

Incrementing a pointer

We like to use a pointer instead of an array in the program, because the variable pointer is incremented, and the array can not increase, because the array is a constant pointer. The following program increments the variable pointer to sequentially access each element in the array:

#include <stdio.h>

const int MAX = 3;

int main ()
{
   int  var[] = {10, 100, 200};
   int  i, *ptr;

   /* 指针中的数组地址 */
   ptr = var;
   for ( i = 0; i < MAX; i++)
   {

      printf("Address of var[%d] = %x\n", i, ptr );
      printf("Value of var[%d] = %d\n", i, *ptr );

      /* 移动到下一个位置 */
      ptr++;
   }
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Address of var[0] = bf882b30
Value of var[0] = 10
Address of var[1] = bf882b34
Value of var[1] = 100
Address of var[2] = bf882b38
Value of var[2] = 200

Decrement a pointer

Similarly, the pointer is decremented and subtracting the number of bytes that is the value of its data type, as follows:

#include <stdio.h>

const int MAX = 3;

int main ()
{
   int  var[] = {10, 100, 200};
   int  i, *ptr;

   /* 指针中最后一个元素的地址 */
   ptr = &var[MAX-1];
   for ( i = MAX; i > 0; i--)
   {

      printf("Address of var[%d] = %x\n", i, ptr );
      printf("Value of var[%d] = %d\n", i, *ptr );

      /* 移动到下一个位置 */
      ptr--;
   }
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Address of var[3] = bfedbcd8
Value of var[3] = 200
Address of var[2] = bfedbcd4
Value of var[2] = 100
Address of var[1] = bfedbcd0
Value of var[1] = 10

Compare pointer

Pointer can be used to compare the relationship between operators, such as ==, <and>. If p1 and p2 points to two related variables, such as with an array of different elements, it can be the size of p1 and p2 compared.

The following procedure modifies the above example, as long as the address of the last element of a variable pointer is the address of the array is less than or equal to & var [MAX - 1], put the pointer variable is incremented:

#include <stdio.h>

const int MAX = 3;

int main ()
{
   int  var[] = {10, 100, 200};
   int  i, *ptr;

   /* 指针中第一个元素的地址 */
   ptr = var;
   i = 0;
   while ( ptr <= &var[MAX - 1] )
   {

      printf("Address of var[%d] = %x\n", i, ptr );
      printf("Value of var[%d] = %d\n", i, *ptr );

      /* 指向上一个位置 */
      ptr++;
      i++;
   }
   return 0;
}

When the above code is compiled and executed, it produces the following results:

Address of var[0] = bfdbcb20
Value of var[0] = 10
Address of var[1] = bfdbcb24
Value of var[1] = 100
Address of var[2] = bfdbcb28
Value of var[2] = 200

C Pointer C Pointer