The new C + + array and pointers are known

Source: Internet
Author: User

#include <stdio.h>using namespacestd;intMainintARGC, _tchar*argv[]) {    inta[5]; intb[4] = {1,2,3,4}; CharC[] ="1234"; int*d=New int[4]; *d =1; * (d+1) =2; * (d+2) =3; * (d+3) =4; printf ("a length =%d\n",sizeof(a)); printf ("b length =%d\n",sizeof(b)); printf ("c length =%d\n",sizeof(c)); printf ("d length =%d\n",sizeof(d));    Delete D; D=nullptr; System ("Pause"); return 0;}

The initialization of an array can be used in the following ways:

1. Declare it directly and assign it later.
2. When declaring the initial value, with "{}" to give the initial value, if it is a char type, you can use {"A", "B", "C"} or directly with the string to assign the value "ABC";
3. With a pointer declaration, the size of the dynamic allocation of the array, in the case of the definition of the pointer from the heap to request 4 int memory to represent the array, but also can be used when the new application to allocate memory, and such an array is required to manually memory release, please note the following delete.

Operation Result:

The size of a and B in the run results equals the length of the sizeof (type) *length type multiplier group, and the algorithm for the size of C is the same, just because the character array will save the end character of the string with a byte length in the Last Face, sizeof (d) = = 4 is not because sizeof (int) = = 4, this 4 represents only the size of the pointer, because in a 32-bit system is 4 bytes to represent a pointer, that is, whether it is char* short* pointer, the size is 4.

If you use a dynamic request for memory to represent a string, in the last one Plus ' \ ', the system will not be active like char[n] this way, will be in the n+1 bit to fill a "";

#include <stdio.h>using namespacestd;intMainintARGC, _tchar*argv[]) {    Char*a=New Char[4]; *a ='a'; * (A +1) ='b'; * (A +2) ='C'; * (A +3) ='D'; We should * (a+3) = '//* ' (a+4) = ' + '//This way is absolutely not allowed
printf ("a strlen =%d\n", strlen (a)); Delete A; A=nullptr; System ("Pause"); return 0;}

Run results

16

Very show that this is not the result we want, we are hoping to get the length of this a is 4, then we have to solve, we can only use the way in the comments, active in the last one to fill a '

The relationship between an array and a pointer

intMainintARGC, _tchar*argv[]) {    CharA[] ="ABCDE"; Char*P1 =A; Char*P2 = &a[0]; Char*P3 = &a[1]; printf ("*p1 =%c, *p2 =%c\n", *p1,*p2); printf ("* (p1+2) =%c, * (p2+2) =%c * (p3-1) =%c\n", * (p1+2), * (p2+2), * (p3-1)); System ("Pause"); return 0;}

Operation Result:

*P1 = A, *p2 = a* (p1+2) = C, * (p2+2) = c * (p3+1) = a

The pointer makes it easy to lock a position in an array and then walk back and forth from the current position of the pointer.
The array corresponds to an area of memory, and the pointer is to a chunk of memory. The address and capacity of an array will not change over the lifetime, only the contents of the array can be changed, and the size of the memory area pointed to by the pointer can change at any time, and when the pointer points to a constant string, its contents cannot be modified, otherwise it will be error-free at run time.

Pointer constants and constant pointers

Pointer constants: int *CONST p=&a; This pointer is assigned at the time of initialization, and the other addresses are assigned to p after not being fully allowed.
Constant pointer: const int* p; This pointer allows you to modify the address it points to, but it does not allow you to modify the value it points to.

    intA=0, b=1; Const int*p; P=&A; P=&b; *p=2;//Do not allow modification of values pointed to by constant pointers    int*Constp1=&A; int*ConstP2;//not allowed, it must be initializedp2=&b;//not Allowed, p2 is a constant is not allowed as a left value

Pointer constants and constant pointers are used more for the qualification of function parameters.

The new C + + array and pointers are known

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.