#运算符, different pointer types, arrays and pointers, pointer operations, heaps, stacks, static extents, read-only, subscript vs pointers

Source: Internet
Author: User

#运算符: Used to convert macro parameters to strings during the precompilation period

#define Convers (x) #x//NOTE: Double quotation marks are not included.

Different types of pointers occupy the same amount of memory space.

Local variable definition:

A[5];

When printing a[i], a[i] is a random number.

If you define a[5]={1,2};

when printing, a[0]=1;a[1]=2;a[2]=a[3]=a[4]=0;

Array address and array name:

1. The array name represents the address of the first element of the array. a=&a[0];

2. The address of the array needs to take the address symbol & to get (&a)

3. The address value of the first element of the array is the same as the address value of the array.

4. The address of the first element of the array and the address of the array are two different concepts.

A+1: (unsigned int) a+sizeof (*a)//plus an element size.

&a+1: (unsigned int) (&a) +sizeof (*&a)//plus the entire array size.

Pointers and Arrays

The compiler differs from array to pointer processing.

Handle pointer: one-time addressing operation.

Char *p= "HW"

To access P, find the memory space, and then from the memory space get address--based on the address to find HW. (an addressing operation was made)

Handling arrays: No addressing, direct access.

Char " HW "

The operation of the pointer: the operation rule with the integer is

P+n: (unsigned int) p+n*sizeof (*p);

Subtraction between pointers: Note this pointer must be of the same type

P1-P2: ((unsigned int) p1-(unsigned int) p2)/sizeof (type);

Conclusion:

1. When the pointer p points to an element of a homogeneous array:

P+1: Will point to the next element.

P-1: Will point to the previous element.

2. When 2 pointers point to elements in the same array, the pointer subtraction is meaningful, which is the subscript difference of the element to which the pointer points.

Heap area: The requested memory space heap.

Stack area: local variable stack

Static zone: Static variable, global variable space.

Read-only zones: assigning constants and program code space

in Main.cppintA=0;//Global Initialization ZoneChar*P1;//Global Uninitialized ZoneMain () {intb//Stack Area      CharS[] ="ABC";//Stack Area      Char*P2;//Stack Area      Char*P3 ="123456"; 123456 in the constant area, P3 in the stack areaStatic intc =0; Global (static) initialization zone P1=Char*) malloc (Ten);//Heap    }

To find the number of elements in an array:

DIM (a) = sizeof (a)/sizeof (*a);

Subscript vs Pointer:

1. Theoretically, when the pointer moves in a fixed increment in an array, its efficiency is higher than the code generated by the subscript (the multiplication is time-consuming when the index is subscript).

2. The performance is better when the pointer increment is 1 and the hardware has a hardware incremental model.

#运算符, different pointer types, arrays and pointers, pointer operations, heaps, stacks, static extents, read-only, subscript vs pointers

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.