Differences between pointers and addresses in C and C ++

Source: Internet
Author: User

In C and C ++, pointers are a very important part,ProgramYou can use pointers to find the data to be operated and executable functions.Code. Many people will understand the differences between pointers and addresses, but few can tell the difference clearly.

Pointers can be divided into two types: function pointers and data pointers. The biggest difference between a pointer and an address is that a pointer has a type and an address has no type. Of course, we can find functions and data through absolute addresses, but the addresses are of no type and cannot perform arithmetic operations on the addresses, when operations such as arrays are involved, the address auto-increment and auto-increment cannot be used to access various variables of the array. However, by referencing the pointer, you can easily access each element of the array by performing a series of addition, subtraction, and subtraction operations on the pointer.

Therefore, pointers are composed of addresses and types. pointers to data not only record the address where the data is stored in the memory, but also record the data type, that is, the memory occupies several bytes, which is not available to the address. The pointer to the function not only records the entry address of the function, but also records the type of the function, that is, the return value type of the function and the parameter type of the function.

There is also a special data type in C ++, that is, the object, which is a user-defined data type. It contains data and functions. But the object variables declared in the same class template each have a data member, but share a function Member, because for objects with a common class template, their data members may be different, but their function code for processing data is indeed the same, so they share a member function. At this time, the Object Pointer Points to the respective data members.

You must be very careful when using pointers. Never cross the border. Otherwise, unexpected results may occur. In addition, do not use Uninitialized pointers. This will access the memory at the location, which may cause a devastating disaster to the operating system, although robust operating systems generally do not allow this.

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.