Tips on pointers and array names in C Language

Source: Internet
Author: User
In C, pointers and array names can be mixed.
For example
Char * P;
During access, * P is the same as P [0], and * (p + 1) is the same as P [1.
For arrays
Char B [5];
During access, B [0] is the same as * B, and B [2] is the same as * (B + 2.

In general communications (such as serial ports), byte transmission is usually used. For example, float or long int,
4 bytes. My method is to take its address, forcibly convert it to a char pointer, and use it as an array.
Float X;
Sbuf = (char *) & X) [0];
Sbuf = (char *) & X) [1];
Sbuf = (char *) & X) [2];
Sbuf = (char *) & X) [3];
At the time of receiving the message, it was just reversed.

What's more interesting is that for the array form, the array name and the following offset can be changed at will.
Char buff [10];
// Or use char * buff = & buffer;

Buff [3] = 0xaa;
3 [buff] = 0xaa; // It is actually the same, fainting...

Therefore, I think the compiler does this: for expressions like XXX [YYY], it will be converted to * (xxx + YYY ),
Therefore, it doesn't matter if you write XXX [YYY] Or YYY [XXX]. Avoid using SARS. If something happens, you may not be responsible for it...

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.