Array and pointers to BDSM

Source: Internet
Author: User

Look at the code first:

1#include <stdio.h>2 3 intMainvoid)4 {5     intI= About;6     int*p = &i;7p[0] = the;8printf"What's in it?%d\n", p[0]);9printf"What's ' s ' i ' now?%d\n", i);Ten     return 0; One}

Look at the results again:
What's in it? 88
What's ' s ' I ' now? 88
That is, when you point the pointer p to the memory address of I, the behavior of P can behave like an array.

Assigning a value to P[0] is a number 88 for that address and three contiguous memory units behind it.

Because the int type occupies 4 bytes, and one memory unit corresponds to one byte.

The value of I is also the same address, that is, the address of p[0], so the value of I is rewritten.

If you assign a value of p[1], the value of I does not change. But actually assigning a value to P[1] Returns a segment error.

* It is estimated that the memory address is not allowed to write. But the compiler will not find this error.

Array and pointers to BDSM

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.