Differences and relationships between P, & P, and * P

Source: Internet
Author: User

When learning C language, pointer learning is a relatively difficult part, and it is often unable to distinguish * P & P. The following is a test procedure for extracting others.
Below is my short test program
Void test (){
Int A = 8;
Int * P = &;
P = (int *) malloc (N * sizeof (INT ));
// Printf ("% d/N", * P );
// A. cout <& P; // 0x0012ff14
// B. cout <* P; // 8
C. cout <p; // 0x000431c50
}

B sentences output the value of the memory unit pointed to by P. What are the output of Statement A and statement C respectively?
Is a output address of the memory unit occupied by P itself?
Does C output the address of the memory unit pointed to by P?
From the test results, it is easy to see that * P is a pointer variable, which stores the address data and can only store the address. Therefore, when * P is assigned a value, check whether the value on the right of the equal sign is an address. If not, your program will have a bug (maybe you are a good assembler writer, Please handle this pointer variable ).
Yes, as the tester thinks, the output content of & P is the memory unit address of * P pointer variable, and the pointer variable itself stores the memory unit address of variable;
* P can be understood as the data pointing to the memory unit pointed to by the pointer variable. In this example, it is the value of;
P is a pointer variable that can store addresses.
In addition, int * P = & A; equivalent to int * P; P = &;
If you are using a Linux system, you can test the program in kernel to better understand it.
# Include <stdio. h>

Int main ()
{
Int A = 8;
Int * P = NULL;
P = &;
Printf ("P is % P/N", P); // P prints the value of & A, that is, the address of.
Printf ("* P is % d/N", * P); // print the value of.
Printf ("& P is % P/N", & P); // print the address where the pointer P is stored.
Printf ("& A is % P/N", & A); // address of.
Return (0 );
}
Note the following two points:
1. Only the address and value can be transferred to the pointer. Otherwise, a forced type conversion is required.
2. When performing type conversion and value assignment, you should pay attention to the type matching of the value assignment.

 

Differences between pointers and arrays:

Many beginners cannot figure out the relationship between pointers and arrays. I will tell you: there are no
Any relationship! They often wear similar clothes to tease you.
A pointer is a pointer. A pointer variable occupies 4 bytes in a 32-bit system, and its value is a memory address.
The pointer can point to any place, but not any place you can access it through this pointer variable.
An array is an array. Its size depends on the type and number of elements. You must specify the element type when defining an array.
And number. Arrays can store any type of data, but cannot store functions.
Since there is no relationship between them, Why do many people confuse arrays and pointers? Many people think that
The needle is the same as the array. This is related to the C-language books on the market. Almost no book has thoroughly explained this problem,
I understand.

# Include <stdio. h>
Intmain ()
{
Int A [5] = {1, 2, 3, 4, 5 };
Int * ptr1 = (int *) (& A + 1); // The A array is a whole. sizeof (A) = 20; & A indicates the first address of the array, & A + 1 is equivalent to adding 20 bytes.
Int * ptr2 = (int *) (INT) A + 1); // after a is forcibly converted to int type, it is equivalent to an integer plus one, therefore, this is equivalent to adding a byte to the address.
Int * ptr3 = (int *) (a + 1); // This is very familiar to everyone. When array a is used as the left value, it is equivalent to & A [0]. it adds an array length, that is, 4 bytes (integer ). A cannot be the right value.
Printf ("% x, % x, % x", ptr1 [-1], * ptr2, ptr3 );
Printf ("% x, % x, % x", ptr1, ptr2, ptr3 );
Return 0;
}

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.