In C language, why should I specify a type for declaring a pointer?

Source: Internet
Author: User

For example, in C language, why should I specify a type for declaring a pointer?

When I learned 8086 assembly, I found a phenomenon:

For DB-type arrays, Si (source address change register) can obtain the address of the next element in the array once as long as Inc;

For DW-type arrays, Si needs Inc twice to obtain the address of the next element in the array;

I think:

Si can be used as a pointer. Each time Si ++ points to the space address of the next byte.

For an array whose space is not 1 byte, perform Si ++ several times until the number of bytes moved by the pointer is the space occupied by each element in the array.

The minimum unit of memory space is byte, and the minimum distance between pointer movement is 1 byte.

In the C language, the pointer ++ operation does not need to calculate the number of times the pointer needs to be ++, And the compiler automatically calculates the number of times, you can obtain the address of the next element in the array as long as ++ is used, regardless of the type;

Of course, the premise is that the type is specified when the pointer is declared, for example:

Char * PC, STR [] = "12345"; double * PD, num [] = {1, 2, 4, 5}; Pc = STR; Pd = num;

PC ++, which points to the address of the memory space of the next byte.

PD ++, which points to the address of the memory space of the next 8 bytes.

Conclusion:

When a pointer is declared, the pointer type determines the unit distance of the pointer movement, that is, the address of the memory space to move.

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.