Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

Pre-school taking

The animated version of this listening to the book on the example is really good! www.bilibili.com/video/BV1MJ…

Use pointer parameters

Functions that process arrays must know when to start and when to end. The sum() function takes a pointer parameter to indicate the beginning of an array and an integer parameter to indicate the number of elements in the array to be processed. (The pointer parameter also indicates the data type in the array.) But this is not the only way to pass the necessary information to a function. Another method is to pass two Pointers, with the first pointer indicating the beginning of the array (as before) and the second pointer indicating the end of the array. The program also shows that the pointer parameter is a variable, meaning that an index can be used to indicate which element in the array to access.

The instance

#include <stdio.h>
#define SIZE 10
int sump(int * start,int * end);
int main(void)
{
    int marbles[SIZE] = {20.10.5.39.4.16.19.26.31.20};
    long answer;

    answer = sump(marbles,marbles + SIZE);
    printf("The total number of marbles is %ld .\n",answer);
    return 0;
}

/* Use pointer algorithm */
int sump(int * start ,int * end)
{
    int total = 0;

    while (start<end)
    {
        total += *start;    // Add up the values of the array elements
        start++;    // let the pointer point to the next element
    }
    return total;
    
}



Copy the code

The total number of marbles is 190.

Program parsing

The pointer start starts pointing to the first element of the marbles array, so the assignment expression total += start adds the first element (20) to total. The expression Start ++ then increments the pointer variable start to point to the next element of the array. Since start is a pointer to int, incrementing start by 1 increments the size of int.

Notice that the sump () function ends the addition loop in a different way. The sum() function takes the number of elements as the second argument as part of the loop test:

for(i=0; i<n; i++)

The sump() function ends the loop with a second pointer:

​ while (start < end)

Because the test condition of the while loop is an unequal relationship, the last element processed by the loop is the element before the position to which end points. This means that the position to which end points is actually after the last element of the array. C ensures that the pointer to the first position after the array is still a valid pointer when allocating space to the array. This makes the test condition for the while loop valid because the end value of start in the loop is end. Note that function calls using this “out of bounds” pointer are more concise:

Answer = sum (marbles, marbles + SIZE);

Because the subscript starts at 0, marbles + SIZE points to the next place at the end of the array. If end refers to the last element of the array rather than the next position at the end of the array, the following code must be used:

Answer = sum (marbles, marbles + size-1);

This is not recommended

You can also compress the loop body into a single line of code

​ total += *start++;

The unary operator has the same precedence as ++, but the associative law is from right to left, so start++ is evaluated before start. That is, the pointer start is incremented before pointing to. ** using the suffix form (that is, start++ instead of ++start) means that the value at the point to which the pointer points is added to total before incrementing the pointer.

** uses ++start, and the order is reversed, incrementing the pointer first and then using the pointer to point to the value at the position. If (start)++ is used, the value pointed to by start is used first and then incremented, not the pointer. ** This way, the pointer will always point to the same location, but the value at that location has changed. Although *start++ is more common, but

Star (start++) is a little bit clearer.Copy the code

Increment of pointer

#include <stdio.h>
int data[2] = {100.200};
int moredata[2] = {300.400};

int main(void)
{
    int *p1,*p2,*p3;// Define three Pointers

    p1 = p2 = data; // p1 equals p2 equals the first element of the array data
    p3 = moredata; // p3 equals the first element of moreData
    printf("*p1 = %d, *p2 = %d,*p3 = %d\n",*p1,*p2,*p3);// Prints the values of three Pointers
    printf("*p1++ = %d, *++p2 = %d, (*p3)++ = %d\n",*p1++,*++p2,(*p3)++);
    printf("*p1 = %d , *p2 = %d, *p3 = %d\n",*p1,*p2,*p3);

    return 0; } operation result: *p1 =100, *p2 = 100,*p3 = 300
*p1++ = 100, *++p2 = 200, (*p3)++ = 300
*p1 = 200 , *p2 = 200, *p3 = 301
Copy the code

parsing

Only (*p3)++ changes the value of the element

The other two operations point P1 and p2 to the next element in the array, respectively

My little understanding:

(a)++ takes the value of a and increments it, instead of incrementing the pointer to the next element in the array