Array first address fetch address

Source: Internet
Author: User
Tags int size

First, the problem arises

Normal pointers can be altered to cause address offsets:

 #include <iostream>  using  namespace  STD ; Span class= "Hljs-keyword" >int Main (int  argc, Char  *argv[]) {int  a = 6 ; int  *p = &a; //p store an address. PP Store P address, the above code allows p to store the address offset  cout  <<&a<<endl; int  *pp = (int  *) &p; cout  <<p<<endl; (*pp) + = 4 ; cout  <<p<<endl; return  0 ; }

Execution Result:

But the array's first address is not:

 #include < Iostream>  using  namespace  STD ; int  Main (int  argc, char  *argv[]) {int  b[ 5 ] ={111 , 666 , 3     , 4 , 5 };    int  *pos = (int  *) &b;    cout  <<*pos<<endl;    (*pos) + +;    cout  <<*pos<<endl; return  0 ; }

Execution Result:

So...

This indicates that there is a problem with the address at the beginning of the array ...

Print it out, sure enough:

cout<<b<<endl;cout<<&b<<endl;

The first address of the array is a pointer to the address, but this pointer takes the same address as the one stored in it ...

Second, the array first address and the address of the group name

People who have just started to learn should know. The array name is equivalent to a pointer. Points to the first address of the array, and the function name is equivalent to the function pointer, pointing to the function's entry address.

#include<stdio.h>   int main()   {       int a[10];    printf("a:\t%p\n", a);    printf("&a:\t%p\n", &a);    printf("a+1:\t%p\n", a+1);    printf("&a+1:\t%p\n", &a+1);    return0;}

Output:

A:0032fcbc
&a:0032fcbc
A+1:0032fcc0
&a+1:0032fce4

A and &a point to the same piece of address. But they have a different effect after +1. A+1 is the memory size of an element (add 4), and &a+1 adds the memory size of the entire array (add 40).

That a and &a point and &a[0] are the same, but different in nature!

int main()   {       int a[10];       printf("%d\n",sizeof(a));       return0;   }  

This code will output the memory size of the entire array. Rather than the size of the first element, so whether we are contacted, sizeof (a) here A and
&a some of the same?! Yes. &a gets the address of the entire array! Both the array name fetch address is equivalent to the logarithmic group fetch address.

Summarize

In fact both A and &a results are the first address of the array. But their type is not the same.
A represents &a[0], which is the address of the first element of the array. A+1 represents the first address +sizeof (element type).


&a Although the value is the array's first element address. But the type is: type (*) [number of array elements], so the &a+1 size is: First address +sizeof (a).

Description

It should be understood that the array name is the same as the first address of the array, and that the array name is just the "equivalent" pointer . Instead of really a pointer, the array name is just a constant (a constant that values the address of an array's first element), so you cannot do the + + or – operation. And the constant is not the address, and the reason why there is &a, in fact, the meaning of a here is not the original number of the group name, it represents the entire array.

Third, supplementary

Note: For example, the following supplementary excerpts from the reference materials appear in the text, the text content is very good, but the arrangement is very messy, special finishing such as the following.

The first paragraph of code first:

#include <stdio.h>intMain () {inta[5]={0x11121314,0x21222324,0x31323334,0x41424344,0x51525354};int *PTR1=(int *)(&a+1);int *PTR2=(int *)(A +1);printf("%x\n", ptr1[-1],*PTR2);}

The printing results are as follows:

This means that &a+1 spans the entire length of the array, and a+1 simply increments the size of an element at the first address of the array, as described in the previous article.

Think again, assuming that line fifth of the first case is changed to

int*ptr2=(int*)((int)a+1);

What will the printing result be?

Here, consider the storage mode of the data in the computer: The big-endian mode and the small-end mode. Explain:

Big-endian mode (Big_endian): The high byte of the character data is stored in the low address. The low byte of the word data is stored in the high address.


Small end Mode (Little_endian): The high byte of the character data is stored in the high address, while the low byte of the word data is stored in the low address.

In big-endian mode. A is stored in a computer such as the following (from a low address to a high address, a hexadecimal number represents a byte of memory, the same as the same):
0x11 0x12 0x13 0x14 0x21 0x22 0x23 0x24 0x31 0x32 0x33 0x34 0x41 0x42 0x43 0x44 0x51 0x52 0x53 0x54

In small-end mode, a is stored in the computer for example:
0x14 0x13 0x12 0x11 0x24 0x23 0x22 0x21 0x34 0x33 0x32 0x31 0x44 0x43 0x42 0x41 0x54 0x53 0x52 0x51

(int) A indicates that the first address of a is strongly converted to integer data (if it is 0012ff6c, the conversion is still 0012ff6c, but it is not a representation of the address but a
hexadecimal integer number). At this point + 1 represents the integer (int) a plus 1 (into 0012ff6d), and then the result is strong to the shaping pointer, so the data address is 0012ff6d.
That is, the second byte at the beginning of the above store. The output *ptr2 at this time. That is, an integer number of the second to fifth bytes in the output a array store, and if the big-endian mode outputs
12131421, if the small-end mode is output 24111213.

#include <stdio.h>#include <iostream>using namespace STD;intMain () {inta[5]={0x11121314,0x21222324,0x31323334,0x41424344,0x51525354};printf("%p\n%p\n", A,&a);//Values equal    cout<<sizeof(a) <<endl;//A is a composite type int[5]    cout<<sizeof(&a) <<endl;//&a is int (*) [5]    cout<<sizeof(A +1) <<endl;//A+1 becomes a pointer to the second element of the array whose type is int *    cout<<sizeof(&a+1) <<endl;//&a+1 or INT (*) [5]. Type or pointer    //a+1 because a points to an int (but not a normal pointer), add 1 to move int size    //&a+1 because &a points to int[5], plus 1 move int[5] size    printf("%p\n%p\n", A +1, &a+1);}

The results of the execution are as follows:

You can say A+i and &a[i] mean the same, but never say A and &a[0] or a+0 mean the same.

Under the supplementary instructions. On vc6.0, the above sizeof (&A) is the 20, and in GCC, and after the VC version number (such as VS2005 and later version number) is the &a completely as a pointer, sizeof (&A) 4 (32-bit machine)

Four or two-dimensional array transfer

Three ways to do it

(1) int (*p) [5]

(2) int a[][5]

(3) int a[5][5]

References

[1] http://blog.csdn.net/syzobelix/article/details/40054095

Array first address fetch address

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.