arrays, pointers

Source: Internet
Author: User

There are always people who think that arrays are pointers, pointers are arrays, and both seem to be exactly the same thing. I have been naïve to think so before. In fact, things

This is not the case, pointers are pointers, arrays are arrays, and the two are completely different things. The reason we think of an array is a pointer, the pointer is a number

groups, simply because they can all be "in the form of pointers" and "in the form of an array". Let's explain the arrays and pointers separately.

(a) array:

int a[5]; I'm sure everyone knows this is an array with 5 elements inside.


The definition and initialization of an array:

int n = 4;int Arr[n] = {1,2,3,4};
The definition of the above array is the easiest mistake for beginners. Because n is a variable, the number of elements in an array must be a constant.

And if we use the Const modifier, will it make a mistake?? In. c files, the amount of the Const modifier is a read-only variable, noting that it is still a variable, while the. cpp

In a file, a const modifier is a constant that we can use to define the number of arrays.

But if you do it this way you can, as follows:

#define N 10int Arr[n] = {0};
Since the pre-compilation phase identifier n is replaced with 10.

The compiler usually does not allocate memory for ordinary const read-only variables, but instead saves them in the symbol table (about the symbol table, after which I will detail

parsing), the const read-only variable has only one copy during the program's run, and the macro is replaced during the precompilation phase.

Note: 1. once an array is given, a will match the memory of this block, which is a++,--a, which is not correct.

In the blog strlen and sizeof, there is a mention of the array size, here again.

The value of sizeof (a) is 20 under the 32-bit system.

sizeof (A[0]) is 4 under 32-bit systems.

sizeof (A[5]) is 4 under 32-bit system. There is no error. It is important to remember that sizeof is a keyword, not a function, and function evaluation is at run time.

waiting, Key The word evaluation is at compile time, although there is no a[5] this element, but there is no real access to it.

2. in both cases, the array name does not degrade: When the array name is the operand of sizeof or the operand of the single-eye operator &.

Two-dimensional arrays: two-dimensional arrays are actually one-dimensional arrays. int b[2][3]; here b can be seen as an array of 2 elements, each of which is an element containing

An array of 3 elements that are stored in memory.


3. Can the array name be left-and right-valued?? array names cannot be left-valued. when the array name is an lvalue, the compiler will assume that the array name is the first array

Elements address, and this address begins with a piece of memory that is a whole, we can only access an element in the array, but not the array as a

Overall for access. The array name can be an rvalue, as an rvalue, representing the address of the first element of the array.

The difference between 4.a and &a: In the blog of strlen and sizeof, we've done a bit of sorting and distinguishing, here's an example to understand.

int main () {int a[5] = {1,2,3,4,5};int *ptr = (int *) (&a + 1);p rintf ("%d%d", * (A + 1), * (ptr-1)); return 0;}
We can carefully analyze the problem, and see the final output of the results of what is it??

The answer is 2 5. &a+1 is a pointer to an array, pointing to the block of memory behind the array element 5, forcing the type conversion, converting to int *, so the result is 5.

If you do not make a forced type conversion, the second result is 1.

(ii) Pointers

Pointer constants: Take a look at the code below.

int main () {int a = 10;*0X0018FF44 = 20;return 0;}
Note: The address of the first variable under vc++6.0 is 0x0018ff44.

Can the above code change the value of a to 20? The answer is no, because the compiler thinks that 0x0018ff44 is a hexadecimal number and is

Unable to make a solution reference. Therefore, it is important to force type conversions when using pointer constants.

* (int *) 0x0018ff44 = 20, so you can change the value of a to 20.

Then look at the following example:

int main () {int *p = (int *) 0x0018ff44;*p = Null;p = Null;return 0;}

After reading this code, I am sure someone would think *p = NULL; This compilation does not pass. In fact, the null ASCII is 0. You should know that now.

We continue to debug this code under vc++6.0, and find that when executed to *p = NULL, p = 0x00000000. Why is this?? Because P

The address is 0x0018ff44, p inside the saved address is also 0x0018ff44 (its own address), so *p is P (in this program is this),

So changing the *p also changed p.

In the book "C Deep Anatomy ," This issue has been given a very detailed explanation, and interested readers can read it on their own.

A pointer variable is a variable that holds the address of a variable.

When it comes to pointer variables, our minds should flash in three "characters" that are closely related to it------the contents of the pointer variable, the pointer variable

to, the address of the pointer variable.

char ch = ' a '; char *CP = &ch;
Let's understand pointer variables with lvalue and rvalue values.

&ch as the right value, &ch is the address of CH and is the same as the variable stored in the pointer variable CP. As an lvalue is illegal.

The CP, as an rvalue, represents the value of the CP, and as an lvalue represents the block of memory that the CP represents.

&CP, as an rvalue, represents the address of the pointer variable CP, which is illegal as a left-hand value.

*CP, as the right value, represents the content of the space that the CP points to, as an lvalue represents the space that the CP points to.

*cp+1, as an rvalue, represents the character ' B ', which is illegal as a left-hand value.

* (cp+1) as the right value, the contents of the next section of the CP, as the left value, represents the next section of memory of the CP.

++CP, as an rvalue, represents the contents of the next section of memory of the CP, which is illegal as a left-hand value.

cp++ as the right value, which represents the contents of the CP and is illegal as an lvalue.

*++CP, as an rvalue, represents the value of the memory address behind CH, as an lvalue indicates the memory location behind Ch.

*cp++ as the content of the right value CH, as the left value, represents ch this space.

Note: + + has a priority greater than *.

++*CP, as an rvalue, represents the contents of the memory that CP points to plus 1, which is illegal as a left value.

(*CP) + + as an rvalue, represents the value of the memory that CP points to, as an lvalue, illegal.

Note: (*CP) + + is not the same as *cp++.

++*++CP, as the right value, indicates that the contents of the memory pointed to by the CP is added 1, which is illegal as an lvalue value.

++*cp++, as an rvalue, represents the value of the memory space that CP points to plus 1, which is illegal as a left value.

Operations between Pointers:

Pointer-pointer: is the number of elements between two pointers, in units of number, not bytes. The pointer-pointer is premised on the two pointers pointing to the same piece of memory;

The same type. Take a look at the following example:

int main () {int arr[10] = {0};int n = &arr[10]-&arr[0];p rintf ("%d", n); return 0;}
Note: The above code can correctly calculate the size of the array. &ARR[10], take address only, will not be out of bounds, if the changeArr[10], there will be an error.

Speaking of which, it would certainly be said that it would be possible to use the address of the last element of the array minus the address before the first element of the array to find the array large

What about small? The answer is no. Because space is not reserved at the front of the array, space is reserved at the end of the array. So, do not let the pointer point to-1th subscript.

Pointers and arrays here, it must be understood by everyone. Next, I'll compare pointers and arrays to analyze some important things.

(c) Pointers, array contrast analysis:

Look at the following example:

Char arr [ten] = "abcdef"; char *ap = arr+2;
Note: The figure is that the AP is not CP


AP is &arr[2];

*ap is arr[2];

Ap[0] is arr[2]; Note: the Subscript reference for C and the indirect access expression are the same.

Ap+6 is &arr[8];

*ap+6 is arr[2]+6; Note that indirect access takes precedence over the addition operator.

* (ap+6) denotes arr[8];

AP[6] also arr[8];

The &AP is the memory address that holds the AP.

AP[-1] is arr[1].

AP[9] is actually an expression arr[11], but the ARR array has only 10 elements in total, ap[9] This expression crosses the right bounds of the array, but

The translator generally does not detect, so if you write such an expression in the program, there will be unexpected results.

Look at the code below:

int main () {int i,arr[10];for (i = 0;i<=12;i++) {printf ("Love You"); Arr[i]  = 0;} return 0;}

The array in this program has crossed out, causing the program to loop. Of course this is only in the VS compilation environment. Let's look at the picture below to see why the cycle of death




Note: Stacks differ from other data structures in that the stack is first in the stack and is at a high address.

If in VS, just over the reserved space, change the value of I, resulting in a dead loop.

If it is under vc++6.0, arr[9] and I are next to each other. As long as the array crosses out. There will be problems.

If running in the GCC compiler under Linux, it looks like arr[9] and I are next to each other. (I change the condition of the cyclic judgment to i<=10, still the cycle of death)

pointer array with array pointers :

First, we must make clear that the pointer array is an array, and each element is a pointer. The array pointer is a pointer to an array.

For the pointer array, we are not unfamiliar, a parameter in the main function is an array of pointers. such as a given pointer array int *arr[5];


And the array pointer?

Note: For the above array of pointers, what exactly is his type?? It should be int (*) [5], the pointer variable is p, it's important to know that

Forced type conversions are used.

Never assume that a two-dimensional array is a two-level pointer. A two-dimensional array is actually a one-dimensional array, and each element is a one-dimensional array.

The following is an example of a two-dimensional array parameter:

void fun (int arr[2][3]) {    ;} int main () {    int arr[2][3];    Fun (arr);    return 0;}
Arr in the argument is the address of the first element of the array, and is a pointer to an array of size 3. So, the size of the second dimension in the formal parameter is notProvince

A little. , in addition to the parameters given in the code, the formal parameter can also be int[][3], or int (*arr) [3]. remember int arr[][]. And Int**arr both are not

to the.

About declarations and definitions:

Definition: Creates an object, allocates a piece of memory for the object, and takes the name.

Declaration: (1) Tell the compiler that the name has been matched to a piece of memory, and the code below does not use that name.

(2) Tell the compiler that the name is already reserved and don't use it anywhere else.

From the top of the study, we already know that arrays and pointers are different things, so the definition of an array is to declare an array, defined as a pointer to the sound

Ming as the pointer, the others are wrong, let us analyze each one.

Defines an array, declared as a pointer:

Like what:

int arr[] = "abcdef";
In another file, you have the following declaration:

extern char *arr;
What happens if we output arr in the declared file?? program crashes, illustrated below:

The declared pointer only accounts for 4 bytes, and what he sees is 0x61 0x52,0x63,0x64, who may not be a valid address. So it's not right.

Defined as a pointer, declared as an array:

Definition: char* p = "abcdef";

Statement: extern Char str[];


So in the declaration of the file we see the str[0] is 0x00,str[1] is 0x12 ... is not going to get the right results. But if we want to

Output abcdef How to output it??

printf ("%s", * (char * *) str);

Or: printf ("%s", (char *) * (int *) str); turn str into int*, dereference, and strong.

Analysis here, so pointers are pointers, arrays are arrays, do not mess with.

A few more important examples are compiled below:

Example 1:

int A[5][5];int (*P) [4] = Null;p = (int (*) [4]) a;printf ("%d%p", &p[4][2]-&a[4][2],&p[4][2]-&a[4][2]);
Analyze what the result of this program output is.


When output in%d format, the result is 4, this takes advantage of the pointer subtraction that knowledge point, when output with%p, what will output?? It will turn-4 into

Unsigned number output because the address has no negative numbers.

-4 Original code: 10000000 00000000 00000000 00000100

Reverse code: 11111111 11111111 11111111 11111011

Complement: 11111111 11111111 11111111 11111100

So%p format output FF FF FF FC

Example 2:

int a[4] = {1,2,3,4};int *PTR1 = (int *) (&a + 1); int *ptr2 = ((int) a+1);p rintf ("%x%x", PTR1[1],*PTR2);
What will the program output result be?? Below diagram:


So the program output results: 4 20 00 00 00 (Small end mode)

In fact, (int) a+1 is to convert a into plastic and then add 1.

Well, the pointers and arrays will be organized so much, if there is imperfect place, I hope you point out ~ ~ ~

arrays, pointers

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.