C language program to read the title

1. The following program

#include <stdio.h>
int fun(int*x, int n)
{
if (n==1)
return x[1];
else
return x[1]+fun(x+1, n-1);
}

main(){

int array[] = {0,9,1,2},res;

res = fun(array,3);

printf("%d",res);

}

Analysis: By topic we can roughly know this is a recursive summation of program topics, then we need to understand that fun () function in the end is doing.

First, the main function fun () array and passing the array 3, the first address of the array is assigned to a pointer array x, n = 3 is not equal to 0 so fun () returns x [1] + fun (x + 1 , n-1);

x [1] is an array corresponding to the array [1] = 9, then x [1] + fun (x + 1, n-1) = 9 + fun (x + 1,2).

Next we go to find fun (x + 1,2), and here the second time we call fun recursive function is so, then we pass the parameter is x + 1 and 2, because the first time we will array the second element is assigned to the pointer x, x + 1 so that the third element of the array to x and x + 1 is the array [2] = 1, n = 2 and because not equal to 0, so we return is 9 + 1 + fun (x + 2,1)

Finally, we so, n == 1, so that the return array [3] = 2, so the final result is 1 + 9 + 2 = 12

To sum up: a knowledge we need to understand is that each of x + 1 array is actually the first address after a move point to the next element.

2.有以下程序请输出程序运行结果
#include<stdio.h>
int disp(char *str){
    while(*str){//遇到'\0'结束
        putchar(*str++);//str依次往后移动
    }
    return *str;
    
}
int main() {
printf("%d",disp("NAME"));
} 

Seen from the program disp function, while () loop is passed sequentially read the main function of the character, when faced with '0 \' to the end and return str,

The last function ends it returns * str That \ 0, but the function's return type is a numeric type, the final output will be 0, and the string is \ 0 to end mark.

So the final result is NAME0, if output in the form it is NAME% c

 

 

 

 

 

 

 

 

 

 

 

 

 

 

He published 196 original articles · won praise 581 · views 470 000 +

Guess you like

Origin blog.csdn.net/wyf2017/article/details/105155526