Meaning of the array name of C language

Source: Internet
Author: User

1:1-d array int a[5];

A: is the array name. A left value represents all of the space (10x4=40 bytes) of the entire array, andbecause the C language specifies that an array operation should be independent of a single operation, the array cannot be manipulated as a whole, so a cannot do an lvalue A right value indicates the first address of the first element of the array (the No. 0 element of the array, that is, a[0]) (the first address is the starting address, which is the first byte in the 4-byte address). A Do right value equals to &a[0];

A[0]: Represents the first element of the array, which is the No. 0 element of the array. The left value represents the memory space of the No. 0 element of the array (4 consecutive bytes) , and the value of the No. 0 element of the array (that is, the number stored in the memory space corresponding to the No. 0 element of the array) when the right value is done .

&a: is the array name a take address, the literal meaning should be the address of the array. &a cannot do Lvalue (&a is a constant, not a variable and therefore cannot be assigned, so it is not natural to do the lvalue.) &a is the first address of the entire array when the right value is done.

&a[0]: literally that means the first address of the No. 0 element of the array. (make clear [] and & priority, [] priority is higher than &, so a first and [] combined re-fetch address). The left value represents the memory space of the first element of the array , and the value of the first element of the array (that is, the number stored in the memory space corresponding to the first element of the array) is the right value. When you do the right value, &a[0] equals a.

Summarize:

The difference between 1:&a and a is when the right value is: &a is the first address of the entire array, and a is the first address of the first element of the array.

The two are equal in numbers, but have different meanings. Different meanings can cause them to behave differently when they are involved in the operation.

2:a and &a[0] do the right value when the meaning and the value are exactly the same, can be completely substituted with each other.

3:&a are constants and cannot be left-valued.

4:a does an lvalue to represent the entire array of space, so a cannot do the left-hand value.

#include <stdio.h>int main (void) {int a[5]={1,2,3,4,5};    int *p1;    int (*P2) [5];    printf ("P1 =%p \ n", p1);    printf ("P2 =%p \ n", p2);    P1 = A;    P2 = &a;    printf ("P1 =%p \ n", p1);    printf ("(p1+1) =%p \ n", (p1+1));    printf ("P2 =%p \ n", p2);    printf ("(p2+1) =%p \ n", (p2+1)); return 0;}

/****** Running Results

P1 = 0xbfbcda54

P2 = 0xbfbcda5c

P1 = 0xbfbcd99c

(p1+1) = 0xbfbcd9a0

P2 = 0xbfbcd99c

(p2+1) = 0xbfbcd9b0

*******************/

/********* Analysis: * * * *

1:P1 P2 is not initialized when defined, so it belongs to the wild pointer.

2:P1 = A, a array name does an rvalue representing the first element first address, and array A is of type int, so the first element of the first address is also a number of type int, so the type matches. Which means that P1 is a pointer to a number of type int

3:p1+1=p1+4 P1 points to the first element of the array's first address, equivalent to P1 point to the inside of the array, so p1+1 is actually p1+sizeof (array type)

4:p2 = &a The definition of P2 is that int (*P2) [5] can be understood as a pointer to an int [5] type, so P2 is a pointer to an array .

The &a is the address of the array name, which means the address of the array (indicating that the address is an array type), so the P2 and &a types match.

5:P2 + 1 = p2 + 20 because P2 is defined as an address that points to an int [5] type, so

p2+1 = p2+sizeof (int [5]);

6: Pointer +1 is actually the pointer +siezof (pointer type), which is actually defined at the time of the definition, because at the time of initialization

The type that the pointer points to must match the type at which the pointer is defined.

For example

Char a[5];

int *p; P is a pointer to type int

p = A; the type does not match and the compilation does not pass. But if this can be initialized successfully, then the pointer is going to go wrong when it's in the operation.

For example, start P = A's address is 0xb2000000, then p+1 is 0xb2000004, and 0xb2000004 is actually a[3] instead of a[1]

cannot be performed, so the matching of pointer types is primarily to be able to perform the operation.

*******************************************/


Two: two-dimensional arrays

#include <stdio.h>int main (void) {int a[2][5];    int *p1;    int (*P2) [5];    int b = 5;  P1 = A;  Compile error, type mismatch p2 = A;    Compile without error, type match printf ("a =%p \ n", a);    printf ("&a[0] =%p \ n", &a[0]);    printf ("P2 =%p \ n", p2);  printf ("P2 + 1 =%p \ n", p2+1);    Here the value of P2 + 1,P2 does not change to printf ("P2 =%p \ n", p2);    printf ("* (P2 + 1) +1 =%p \ n", * (P2 + 1) +1); return 0;}

/********** Running Results ************

A = 0xbfeaebc8

&a[0] = 0xbfeaebc8

P2 = 0xbfeaebc8

P2 + 1 = 0XBFEAEBDC

* (P2 + 1) +1 = 0XBFEAEBE0

**************************/

/************ Analysis * * * *

1:P2 is the int* [5] Type is a pointer to int [5],

2: The array name does the right value represents the first element of the array first address, the array name of the two-dimensional array represents the address of the first dimension, and the type is int [5]

So it matches the P2 type.

3:p2+1 = P2 +20 The reason is that P2 points to an int [][5], so P2 + 1 actually points to a[1][] which is the second element of the first dimension.

4:* (p2 + 1) +1 = (p1+1) +4, pointing to Yes a[1][1] means * (* (p+i) +j) equals A[i][j].


*/




This article is from the "11664570" blog, please be sure to keep this source http://11674570.blog.51cto.com/11664570/1927647

Meaning of the array name of C language

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.