Pointer, pointer, pointer...

Source: Internet
Author: User

Tag: pointer

1. pointer constants and constant pointers

Int A = 10;
Int * const p1 = &;
Const int * P2 = &;

P1 is a constant that cannot change the address, that is, a pointer constant, but can modify the content it points.

P2 is a pointer to a constant, that is, a constant pointer. The address content it points to cannot be modified, but it can indeed be modified.

2. pointer operation

#include<stdio.h>int main(){int *ip1,*ip2,ivalue;char *cp1,*cp2,cvalue;ip1 = (int*)0x500;ip2 = (int*)0x518;ivalue = ip2 - ip1;cp1 = (char*)0x500;cp2 = (char*)0x518;cvalue = cp2 - cp1;printf ("ivalue=%d  cvalue=%d\n",ivalue,cvalue);return 0;}
Running result: ivalue = 6 cvalue = 24

The value of a pointer is related to its type. The two addresses are also 24 bytes different, and the result of integer pointer Subtraction is 24/4 = 6; the result of character pointer Subtraction is 24/1 = 24;

3. pointers and Arrays

#include<stdio.h>int main(){int a[2][3][4] = {{{101,102,103,104},{111,112,113,114},{121,122,123,124}},{{201,202,203,204},{211,212,213,214},{221,222,223,224}}};printf("%d\t%d\n",**(a[0]+2),sizeof(a));return 0;}
Output result: 121 96

Analysis: For an array A, * A is equivalent to a [0], and * (a + 2) is equivalent to a [2]. Likewise, for multi-dimensional arrays * A [0] is equivalent to a [0] [0], * (a [0] + 2) is equivalent to a [0] [2], ** (A [0] + 2) is equivalent to * A [0] [2] and is equivalent to a [0] [2] [0].
Sizeof (a) calculates the memory space occupied by array A: (2*3*4) * 4 byte = 96 byte.


Pointer, pointer, pointer...

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.