C language pointer variable operation detailed _c language

Source: Internet
Author: User

The pointer variable holds the address, which is essentially an integer that can perform partial operations, such as addition, subtraction, comparison, and so on, see the following code:

#include <stdio.h>
int main () {
  int  a =  *pa = &a, *paa = &a;
  Double b = 99.9, *PB = &b;
  char  c = ' @ ', *pc = &c;
  Initial value
  printf ("&a=% #X, pa=% #X, pb=% #X, pc=% #X \ n", &a, PA, Pb, PC);
  Addition Operation
  pa++; pb++; pc++
  ; printf ("&a=% #X, pa=% #X, pb=% #X, pc=% #X \ n", &a, PA, Pb, PC);
  Subtraction Operation
  PA-= 2; PB-= 2; pc-= 2;
  printf ("&a=% #X, pa=% #X, pb=% #X, pc=% #X \ n", &a, PA, Pb, PC);
  Compare Operations
  if (PA = = paa) {
    printf ("%d\n", *PAA);
  } else{
    printf ("%d\n", *PA);
  }
  return 0;
}

Run Result:

&a=0x28ff44, pa=0x28ff44, PB=0X28FF30, pc=0x28ff2b
&a=0x28ff44, Pa=0x28ff48, pb=0x28ff38, pc=0x28ff2c
&a=0x28ff44, Pa=0x28ff40, Pb=0x28ff28, PC=0X28FF2A
2686784

From the results of the operation can be seen: PA, Pb, PC plus 1 each time, their address increased by 4, 8, 1, exactly the length of int, double, char type, minus 2 o'clock, the address is reduced by 8, 16, 2, exactly the length of int, double, char type twice times.

It's odd that the result of the pointer variable subtraction operation is related to the length of the data type, not simply adding 1 or minus 1.

In the case of a and PA, the type of a is int, occupies 4 bytes, and the PA is a pointer to a, as shown in the following illustration:

At the beginning, the PA points to the beginning of a, moving 4 bytes backward from the point where the PA points, and the 4-byte content as the data to be fetched, which is exactly the memory occupied by variable A, when reading the data by *PA.

If pa++, add 1 to the address, it becomes the pointing relationship shown in the following figure:

This time the PA points to the middle of the integer A, *PA uses a red dotted line of 4 bytes, of which the first 3 are variable A, and the next 1 are other data, and "mixing" them together obviously has no practical significance, and the data obtained will be very strange.

If pa++, so that the address plus 4, just can completely skip the integer a, pointing to the memory behind it, as shown in the following figure:

We know that all the elements in an array are contiguous in memory, if a pointer points to an element in an array, then plus 1 means pointing to the next element, minus 1 means pointing to the previous element, so the pointer subtraction operation has a realistic meaning, which we'll delve into in the C-and-A-series section.

But the C language does not specify how variables are stored, if you define multiple variables in succession, they may be next to each other, or they may be dispersed, depending on the type of variable, compiler implementation, and specific compilation mode, so for pointers to common variables, we often do not add and subtract operations, although the compiler does not error , but it doesn't make sense because you don't know what data it is pointing to later.

The following example is a warning to the reader not to try to get the address of the next variable through the pointer:

#include <stdio.h>
int main () {
  int a = 1, b = 2, c = 3;
  int *p = &c;
  int i;
  For (i=0 i<8; i++) {
    printf ("%d,", * (P+i));
  }
  return 0;
}

The results of the run in VS2010 Debug mode are:

3,-858993460,-858993460, 2,-858993460,-858993460, 1,-858993460,

You can see that variables a, B, C are not next to each other, they are mixed with other auxiliary data.

Pointer variables can participate in comparison operations in addition to the addition and subtraction operations. When a pointer variable is compared, the value of the pointer variable itself, which is the address of the data, is compared. If the address is equal, then two pointers point to the same data, or they point to different data.

The above code (the first example) when comparing the values of PA and PAA, the PA has pointed to the previous data of a, so they are not equal. The previous data of a does not know what it is, so it causes printf () to output a meaningless number, which confirms the view above and does not add subtraction to pointers to ordinary variables.

In addition, it is necessary to explain that the pointer variable can not be multiplication, division, and other operations, in addition to a grammatical error, there is no practical meaning.

The above is the C language pointer variable operation of the detailed data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.