Learn C Nineth Day (pointer arithmetic, array pointer)

Source: Internet
Author: User

1. Pointer addition and subtraction operations

1) pointer + integer will move backward sizeof (pointer type) * Whole number of memory units

2) pointer-integer will move forward sizeof (pointer type) * Whole number of memory units

3) Description: The compiler will not check whether the destination address is available after the move, if the move error, may modify the memory unit should not be modified, so this operation is appropriate in the array, or the dynamic application of memory.

4) The result of subtracting the same type pointer is: (value of pointer 1-value of pointer 2)/sizeof (pointer type)

#include <stdio.h>voidMain () {intA =Ten; int*p = &A; //the address that the p+1 points to is not initialized, and the compiler does not check that the destination address is available after the moveprintf"%d", * (P +1));//-858993460    intnums[Ten] = { A, +, $,234,324, $, the, the, the, at};//Subtraction of pointers: the number of adjacent elements between the twoprintf"\n%d", &nums[9]-&nums[3]);//6GetChar ();}

2. Pointer size comparison If the elements pointed to by two pointers are in the same array (or in the same block of dynamically requested memory), the pointer size comparison reflects the element's precedence in the array. 3. Understanding of pointer Self-amplification (p++ and ++p)
#include <stdio.h>voidMain () {intnums[Ten] = {Ten, -, A, the, One,3, at,111,344,222}; int*p =Nums;  while(p++ < Nums +2)    {        // theprintf"%d\t", *p); //p starts with the first address of the Nums,//after the parenthesis is out of the while, p moves back one element, pointing to nums[1], so the value of output NUMS[1]//then nums+1<nums+2, after the parentheses after the while, p moves one element backward,//point to nums[2], so the value of output NUMS[2]    }    //While (++p < Nums + 2)//{    //    // -//printf ("\n%d\t", *p); //p starts with the first address of the Nums,//after executing the parentheses after a while P moves back one element, pointing to nums[1], so the value of output NUMS[1]//after executing the parentheses after the while, p moves one element backward, and nums+2<nums+2 does not, so the end//}GetChar ();}
4. Priority issues () >++>*

Understanding of *p++, (*p) + +, ++*p, + + (*p)

#include <stdio.h>voidMain () {Doublenums[Ten] = {Ten, -, A, the, One,3, at,111,344,222}; Double*p =Nums; //p++ is a pointer typeprintf"%d\n",sizeof(p++));//4//*p++ is a double typeprintf"%d\n",sizeof(*p++));//8//p at first point to nums[0],*p++ first fetch the value of *p, out of the expression P point to nums[1]printf"%f\n", *p++);//Ten//the pointer moves backwards and does not change the data of the num[0]printf"%f\n", nums[0]);//Ten//first Take out the value of *p 44, then *p=45,printf"%f\n", (*p) + +);// -//changed the data of nums[1] due to *p=45printf"%f\n", nums[1]);// $//p at first point to nums[1],++p and point to nums[2]printf"%f\n", *++p);// A//first remove the value of the *p 22,++22=23printf"%f\n", ++*p);// at//first remove the value of the *p 23,++23=24,*p=24printf"%f\n", + + (*p));// -//*p=24,nums[2] Data has been changedprintf"%f\n", nums[2]);// -GetChar ();}
5. Pointers and Arrays

1) Using pointer variables to point to array elements

The array name represents the address constant of the first address of the array, assigning the array name to the pointer variable, except that the first address of the array is assigned to the pointer variable, not to the address of all the elements

  #include <stdio.h>void   Main () { int  num[10  ];  int  * p = num; //     point to the first address of the array  int  * p1 = &num[//  point to array element  //  printf ( %x\t%x  , num, &num); // fdfec8 Fdfec8   GetChar ();}   

2) Array element representation method

#include <stdio.h>voidMain () {intnum[4] = {1,2,3,4}; int* p =num;  for(inti =0; I <4; i++) {printf ("%d\t", Num[i]); printf ("%d\t", * (num+i)); printf ("%d\t", P[i]); printf ("%d\t", * (P +i)); } getchar ();}
6. Array pointers

Array pointers (also called row pointers)
define INT (*p) [n];
P is a pointer to an integral type of one-dimensional array, the length of the one-dimensional array is n, when the p+1 is executed, p crosses the length of n integer data.

#include <stdio.h>voidMain () {//Two-dimensional arrays are also linearly arranged in memory    inta[3][4] = { the,234, $, at, Wu, the, About, -, A,654, at, A }; //p is a pointer to a one-dimensional array of integral types, and the length of this one-dimensional array is 4    int(*p) [4] = A;//array pointers (row pointers)//represents the beginning of the No. 0 addressprintf"%x\n", *p);//18F8DCprintf"%x\n", &a[0]);//18F8DCprintf"%x\n", a);//18F8DCprintf ("%d\n",sizeof(*p));// -printf"%d\n",sizeof(p));//4printf"%d\n",sizeof(p[0]));// -//*p + 1 represents the column pointerprintf"%d\n",sizeof(*p +0));//4//P equivalent a[0], p+1 equivalent a[1]printf"%x\t%x\t%x\n", p, p +1, p +2);//1df7b4 1df7c4 1df7d4printf"%x\t%x\t%x\n", a[0], a[1], a[2]);//1df7b4 1df7c4 1df7d4     for(inti =0; I <3; i++)    {         for(intj =0; J <4; J + +)        {            //two-dimensional array elements represent the FAprintf"%d\t", P[i][j]); printf ("%d\t", * (* (p + i) +j)); printf ("%d\t", * (P[i] +j)); printf ("%d\t", A[i][j]); printf ("%d\t", * (* (A + i) +j)); printf ("%d\t", * (A[i] +j)); } printf ("\ n"); } getchar ();}

Description

1) line pointer every plus 1, go one line

A represents the No. 0 address

A+i represents the first address of the beginning

2) column pointers each add 1, walk 1 columns

A[0] Address of a[0][0]

A[I]+J representative A[i][j] Address

3) * (* (a+i) +j) represents the value of the a[i][j] element, while * (A+i) +j represents the address of a[i][j]

Learn C Nineth Day (pointer arithmetic, array pointer)

Related Article

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.