Some difficult things about the "extended Knowledge 3" array

Source: Internet
Author: User

Some difficult things about the "extended Knowledge 3" array

Extended Directory

1. &array+ 1

2. array+1

3. &array[0]+ 1

Questions about &array+, array+ 1 and &array[0]+ 1 are particularly difficult to understand ~-~. So I'll explain it today.

Since the storage unit for each element in the array is continuously allocated, you can access the array as a pointer, which is the first address of the number.

such as: intarray[100];

Array is the first address of the array with values equal to & Array[0]

PS: Through the address, you can quickly and easily access the array of other element values, the method is as follows:

First address + offset

The value of the element I can be A * (Array+i), * (&array[0]+i), array[i]

Array is the first address of the first element of the array , with the &array[0] Same, while &array is to take the first address of the array.

[ program 1]

#include <stdio.h> int main (void) {         int array[5]= {1, 2, 3, 4, 5};                 printf ("        array[1]=%d\n", array[1]);         printf ("        * (array+1) =%d\n", * (array+ 1));         printf ("* (&array[0]+ 1) =%d\n", * (&array[0]+ 1));                 return 0;}

Operation Result:

array[1]= 2

* (array+1) = 2

* (&array[0]+ 1) = 2

[ program 2]

#include <stdio.h> int main (void) {         int array[5]= {1, 2, 3, 4, 5};                 printf ("        array[1]=%d\n", array[1]);         printf ("        * (array+1) =%d\n", * (array+ 1));        printf ("     * (&array+ 1) =%d\n", * (&array+ 1));//cross-border!!!                  return 0;}


Operation Result:

array[1]= 2

* (array+1) = 2

* (&array[0]+ 1) = 2293440

Analysis:

Array: Is the first address of the first element of the arrays, that is, the first address of array[0]

&array: The first address of the array is taken.

&array+ 1:

Take the first address of the array, the value of this address plus sizeof (array), which is &array+ 5* sizeof (int), and the first address of the next array .

array+ 1://equivalent to &array[0]+ 1;

is to take the first address of the next element of the array, which is array[1].

The address of &array+ 1 was found to be out of bounds, the address of which is array[5], i.e. &array+ 1= &array+ 5* sizeof (int). The address of array+1 and &array[0] is the address of array[1].

"Smile at the Fingertips" error is unavoidable, hope to get everyone's correction ^ ^

When reproduced, keep the original link http://codingit.howbbs.com and http://blog.csdn.net/mirrorsbeyourself

Some difficult things about the "extended Knowledge 3" array

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.