C + + pointers explaining and places to note

Source: Internet
Author: User




int *x =&10;//cannot fetch address to 10 because he is not in the stack area//stack constant Area B can take the address of the variable

10 is a literal that can't take an address


1: Pointer is a data type

1) The pointer is also a variable, occupying memory space, used to maintain memory address, in general, all pointers,

are 4 bytes;

2) *p operating memory

When the pointer is declared, the * number indicates that the variable being declared is a pointer,

When the pointer is used, the * number represents the value of the memory space pointed to by the manipulation pointer,

*p is equivalent to finding a piece of memory through the value of the address {p variable} and then manipulating the memory

*p on the left side of the equal sign (assigning values to memory)

*p on the right side of the equal sign (Gets the value from memory)


3) A pointer variable and the memory block it points to are two different concepts;


Char s= ' a '; char *p=&s;*p= ' b ';//This operation will not change the value of the pointer variable, but will change the value of the address pointed to


Ensure that the memory pointed to is modifiable


char*p= "Abcdeff"; The C + + compiler will do two operations to give the string a memory and then assign p 4 bytes of memory
Then assign the address of a to *p, which is the first address of the string.


Char *p=null;

strcpy ([, "ABCDE"); You cannot write data to empty memory


4) How to understand (multilevel) pointers do function parameters

A. At the angle of the C + + compiler, if the parameter compiler of the pointer is only allocating four bytes of memory

For example:

/*

void sed (char *p1); void sed (char* p1)

void Send (CHAR**P1); void Send (char * * p1);

void Send (char ***p1); void send (char *p[])

void Send (char (*P) [ten]); void Send (char ******p);

*/


The maximum meaning of the pointer when two indirect assignments are present

The 1 pointer variable and the memory space he points to are different.

int a=10;

a=15; Direct Assignment

int *p=null;

*p=20; *p on the left is indirectly modifying the value of a to reflect a small meaning


But if you use the function to pass the parameter, it is different.


int Ge (INT*P)
{
*p=30;
}
int main ()
{
int a=10;
Ge (&a)
}

There's something to add next time.



C + + pointers explaining and places to note

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.