[Extended knowledge 3] array problems and array expansion difficulties

Source: Internet
Author: User

[Extended knowledge 3] array problems and array expansion difficulties

[Extended knowledge 3] array difficulties

 

Extended directory

1. & array + 1

2. array + 1

3. & array [0] + 1

 

The problems related to & array +, array + 1, and & array [0] + 1 are particularly difficult to understand ~ -~. So I will explain it today.

Because the storage units of each element in the array are continuously allocated, you can use pointers to access the array. The array name is the first address of the number.

For example, intarray [100];

Array is the first address of the array, and its value is equal to & array [0]

PS: Through the Receiving address, you can quickly and conveniently access other element values in the array, as follows:

First address + offset

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

 

Array is the first address of the first element of the array, which is the same as & array [0], and & array is 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;}

Running result:

Array [1] = 2

* (Array + 1) = 2

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

 

[Procedure 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 ;}


Running result:

Array [1] = 2

* (Array + 1) = 2

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

 

Analysis:

Array: the first address of the array's first element, that is, the first address of array [0 ].

& Array: the first address of the array.

 

& Array + 1:

Take the first address of the array. The value of this address is added with sizeof (array), that is, & array + 5 * sizeof (int), which is also the first address of the next array.

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

Is the first address of the next element of the array, that is, the first address of array [1.

 

:

It is found that the address of & array + 1 is out of bounds. It is the address of array [5], that is, & array + 1 = & array + 5 * sizeof (int ). The addresses of array + 1 and & array [0] are the addresses of array [1.

 

[Smile at your fingertips] errors are inevitable. I hope you can get your corrections ^-^

Reprinted when retaining the original link http://codingit.howbbs.com and http://blog.csdn.net/mirrorsbeyourself

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.