arrays and pointers

This article mainly talks about arrays and pointers. The reason why I want to put them together is because when explaining arrays, we must talk about pointers, so that we can integrate and use them flexibly. When you don't know what's going on, it's normal to make mistakes, and you won't change it. Well, let's get to the point:

An array is a bunch of contiguous memory spaces of the same type, each of which can store an element of the type of the array. First of all, if you want to use an array, since you know that it is used to store data, you must need space, that is, you must declare it first and ask the computer for space. E.g:

int main(void)
{
    int a[3];
    
    return 0;
}

There is only one sentence in this code, int a[3]; This sentence is to tell the compiler to take a space in memory for me to use, this space is three consecutive int type spaces, this space It's up to you. Pay attention when using it. You can directly use the array name to add subscripts to the array to operate on the elements in the array, but the subscripts start from 0, and there are a total of n to n-1. Just like the above array, its three elements are represented by a[0], a[1], a[2] respectively. If the subscript you use in the program exceeds the range of 0 to 2, when compiling There will be no error, the compiler will not check this kind of error, but it will make an error when running. The error reported on linux is a segmentation fault. The segmentation fault means that you are using an undefined space, that is, you are using a space that does not belong to you. , well, look at the following code

#include<stdio.h>
int main(void)
{
    int a[3],i;
    for(i = 0; i < 3; i++)
        scanf("%d",&a[i]);
    for(i = 0; i < 3; i++)
        printf("%d ",a[i]);
    printf("\n");

    return 0;
}

Arrays are generally processed with loops. One loop is used for one-dimensional data, and two loops are used for two-dimensional arrays (described below). In the loop, i starts from 0 and ends at n-1, which just corresponds to the subscript of the array. In the first loop, each loop assigns values ​​to a[0], a[1], a[2], three of them represent three variables, so it is essential to take the address character &, and then The second loop is to output three numbers in turn. When the reader is trying the code, each number is separated by a space when inputting, and finally ends the input with a carriage return;

Arrays and pointers are very closely related. If you don't understand pointers, please read my other article first, pointers http://www.cnblogs.com/wangweigang/p/8990237.html

The array is in the program, and the compiler will optimize it into a pointer. When you declare the int a[3]; array, the array name represents the address of the first element, that is, the array name is the pointer to pointer to the first element of this array

#include<stdio.h>
int main(void)
{
    int a[3],i;
    for(i = 0; i < 3; i++)
        scanf("%d",&*(a+i));
    for(i = 0; i < 3; i++)
        printf("%d ",*(a+i));
    printf("\n");

    return 0;
}

In this program, I replaced a[i] with *(a+i), the compilation and operation are exactly the same as above, there is nothing wrong, and it also verifies what I just said, so in the program, there can be many kinds of expressions array method

#include<stdio.h>
int main(void)
{
    int a[3],i;
    int *p = a;
    
    for(i = 0; i < 3; i++)
        scanf("%d",&a[i]);
    for(i = 0; i < 3; i++)
        printf("%d ",a[i]);
    printf("\n");
    
    a[2] = 10;
    printf("%d\n",a[2]);
    2[a] = 11;
    printf("%d\n",2[a]);
    
    p[2] = 4;
    printf("%d\n",p[2]);
    2[p] = 5;
    printf("%d\n",2[p]);

    return 0;
}

There is nothing wrong with this program, and the running result is what you imagined. Maybe you have some doubts. You said that a[i] and *(a+i) can be interchanged. My pointer p points to the array a. I Of course, you can either *(p+2) or p[2], these are no problem, but what the hell are you 2[a], 2[p], don't be surprised, because I said, the compiler in the array program will be replaced with pointers. Well, there is no difference between *(a+2) and *(2+a). Similarly, there is no difference between my a[2] and 2[a]. The compiler is in the process of compiling. It only converts the form of square brackets into understanding address symbols *, parentheses and plus signs, so if you find such code 2[a], 2[p], 0[a] when you look at other people's programs Don't be surprised, it can only mean that the people who write the code are naughty.

By the way, since each element of this array is a variable of type int, the pointer can be defined as a pointer to an int variable. Understand this in my other article on pointers and two-dimensional arrays for pointers to two-dimensional arrays. The anatomy of the pointer declaration is of great help.

Well, you may have to ask here, I can use arrays, and you can tell me what pointers are used for. Wouldn’t it be superfluous if I wrote it like the above in the program.

This is where the function is involved. In the process of passing parameters to the function, the array will become a pointer, and the function can only change one variable, that is, use the return value to assign it to this variable. If you want to change multiple variables, you have to use a pointer. Please see this program

This program is a homework problem of the teacher, which requires three functions to realize the input, sorting and output of the array. In this program, you will find that during the input and sorting, the elements in the array are changed in other functions. value, the value of the array of the main function will also change. Referring to what I said above, the array is a pointer when passing parameters, and then the pointer is used for indirect assignment, so the value of the array in the original function will be changed. So in future programs, as long as you see that the pointer points to the array or the array is a parameter, you can use two representation methods to operate, and you can use whichever you are used to.

Pointers and arrays are here first. More about pointers and arrays will be introduced in detail in my pointers and two-dimensional arrays.

Original, reprint and indicate the source, thank you! !

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325376411&siteId=291194637