Some tips about memory operations in C

Source: Internet
Author: User
Assume that an array A contains all the int elements, and the array has three elements.

The expression A + 1 is equal to a [1].

In this case, if an int is stored in two bytes in the system, the expression A + 1 actually uses

The address of a is moved two byte backward. If you want to move the address of a to one byte backward, you can do this.

Unsigned char * P =;

P + 1 ---------------- in this case, the address of a is moved back to 1 byte, because the unsigned char is stored as 1 byte.

There may be a question: What is the difference between unsigned char and Char? As we all know, char is actually an ascii code, which is stored in 8 bits, but the maximum bit is not considered. That is to say, only 1-is defined, more than 128 of the ASCII code is not defined (defined as extended characters); but for unsigned char, more than 128 characters can also be recognized, that is, they will be displayed as extended characters, that is to say, if the highest bit is 1, unsigned char can also be displayed and defined.

In addition, for struct in C, when we reference its internal element, we should not use the address offset method, which should be defined in C syntax. # And-> Number, Because struct has an address alignment when stored in the memory. If we use the struct base address offset backward to get the element, the problem may occur.

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.