66. Write a string copy of the program (handled with pointer variables).
1#include <stdio.h>2 3 voidMainvoid)4 {5 Chara[ -], b[ the], *p, *Q;6 7p =A;8Q =b;9printf"Input Data:");Ten gets (a); One while(*p! =' /') A*q++ = *p++; -*q =' /'; - puts (b); the}
View Code
Mark:
After the copy operation is finished, you need to add "\" after the valid character of the B array, otherwise garbled when the B array is output by string.
67. Program that writes string connections (handled with pointer variables).
1#include <stdio.h>2 3 voidMainvoid)4 {5 Chara[ the], b[ -], *p, *Q;6 7p =A;8Q =b;9 gets (a);Ten gets (b); One while(*p! =' /') Ap++; - while(*q! =' /') -*p++ = *q++; the*p =' /'; - puts (a); -}
View Code
68. (1) Define a two-dimensional array a[3][2].
The C language specifies that a represents the address of a[0][0], a+1 represents the address of the first element a[1][0] in the next line, and a+2 represents the address of a a[2][0].
int (*p) [2] is defined as p as the row pointer, the change of P is done by row, and the value in [] must be the number of columns (that is, the number of elements per row of the array) that the pointer variable p refers to.
If P does not point to a or the definition (*P) [2], the value in [] is not the same as the column of a array, you cannot use p[i][j] instead of a[i][j].
(2) When working with a two-dimensional array, the C language is treated as a one-dimensional array that contains special elements, which are both a one-dimensional array. If defined "char a[3][4];" , the two-dimensional array A is considered to be a one-dimensional array consisting of 3 elements a[0],a[1],a[2], and each element can be thought of as a one-dimensional array of 4 characters.
C language Provisions, a[0],a[1],a[2] represent first, second, the first address of the third line, so the program "for (i = 0; i < 3; i++) puts (a[i]);" Equivalent to "for (p = A; p < A + 3; p++) puts (p);".
69.int a[3][2], (*P) [2]; p = A;
(1) A two-dimensional array a element can be represented in the following form:
A[I][J], p[i][j], (* (A+i)) [j], * (A[I]+J), * (P[I]+J), * (* (a+i) +j), * (* (p+i) +j). (where 0 <= i < 3, 0 <= J < 2)
(2) The address of a element of a two-dimensional array can be represented in the following form:
&A[I][J], &p[i][j], & ((* (A+i)) [j]), & (((* (P+i)) [j]), A[i]+j, P[i]+j, * (a+i) +j, * (P+i) +j. (where 0 <= i < 3, 0 <= J < 2)
70. Write the function Myswap1, which implements the exchange of two values.
1#include <stdio.h>2 3 voidMYSWAP1 (intXinty)4 {5 intZ;6z =x;7x =y;8y =Z;9 }Ten One voidMain (void) A { - intx =3, y =5; - theprintf"before:x =%d, y =%d\n", x, y); - myswap1 (x, y); -printf"after:x =%d, y =%d\n", x, y); -}
View Code
Results:
Before:x = 3, y = 5
After:x = 3, y = 5
Explain:
When executing the statement "Myswap1 (x, y);" , the process of the program to the MYSWAP1 function, then the system for two parameters x, y allocation of two storage units, while the actual parameter x, Y value 3 and 5 are passed to the corresponding parameter x, y, in the function to exchange the values of the parameters × and y, when the function of the last "}" called the end, parameter x, The storage unit occupied by Y is released and the parameter x, Y disappears. The function call is complete, the process goes back to the place where the MYSWAP1 function is called in the main function, and then proceeds to execute the statements following it, outputting the values 3 and 5 of the X and Y.
In the C language, the data is passed "by value", that is, the data can only be passed from one direction to the formal parameter, and the change of the parameter value does not affect the value of the corresponding argument. However, when a parameter is a pointer variable that can point to an argument, the parameter can affect the argument. Rewrite the function myswap2 as follows.
1#include <stdio.h>2 3 voidMYSWAP2 (int*x,int*y)4 {5 intZ;6z = *x;7*x = *y;8*y =Z;9 }Ten One voidMain (void) A { - intx =3, y =5; - theprintf"before:x =%d, y =%d\n", x, y); -MYSWAP2 (&x, &y); -printf"after:x =%d, y =%d\n", x, y); -}
View Code
Results:
Before:x = 3, y = 5
After:x = 5, y = 3
71. Write the function to find the value of 1-1/2+1/3-1/4+...-1/n.
1#include <stdio.h>2 3 floatMycal (intn);4 5 voidMain (void)6 {7 intN;8 floatsum;9 Tenprintf"Input Data:"); Onescanf"%d", &n); Asum =mycal (n); -printf"1-1/2+1/3-1/4+...-1/n =%f\n", sum); - } the - floatMycal (intN) - { - intI, sign =1; + floatsum =0.0; - + for(i =1; I <= N; i++) A { atsum = sum + (float) Sign/i; -Sign =-Sign ; - } - returnsum; -}
View Code
Although the mycal function is defined after the main function, but because the function prototype described "float mycal (int n);", the function is allowed to be called, and the prototype description uses the "First copy of the function, plus a semicolon" method.
C Language Growth Learning topic (15)