2. Exercises under the hall
1. Strings and pointers
Please write the results of the following program
#include<stdio.h>
int main( )
{ char string[30]="How_are_you" ;
char *p=&string[0], *p2=string+8;
printf("%s,%s\n" , p , p2 ) ;
}
The running result of the program is:
#include <stdio.h>
int main()
{
printf("_______________________");
}
Idea: * p is a pointer declaration symbol, and the table name p is a pointer variable. When assigning the address of a variable or the first address of a pointer to a p variable, it means that the pointer variable of p points to this value. The string is read until the end.
Answer: How_are_you, you
2. The pointer
problem in the array : The following array definitions are provided: int a [3] [4] = {{1,3,5,7}, {9,11,13,15}, {17,19,21 , 23}}; Calculate the values of the following items (set the first address of array a to 2000, and an int type number occupies four bytes).
Write a program and output one line per request.
(1)a[2][1] (2)a[1] (3)a (4)a+1
(5)*a+1 (6)*(a+1) (7)a[2]+1 (8)*(a+1)+1
(9)*(*(a+2)+2)
answer:
#include<stdio.h>
int main(){
printf("19\n");
printf("2016\n");
printf("2000\n");
printf("2016\n");
printf("2004\n");
printf("2016\n");
printf("2036\n");
printf("2020\n");
printf("21\n");
return 0;
}
analysis:
数组: 数组下标:
1 3 5 7 00 01 02 03
9 11 13 15 10 11 12 13
17 19 21 23 20 21 22 23
(1)a[2][1]的值
(2)a[1],代表第二行的首地址,2000+4*4=2016
(3)a,代表a[0][0]的地址,2000
(4)a+1,代表a[1][0]的地址,2016
(5)*a+1,代表a[0][1]的地址,2000+4=2004
(6)*(a+1) ,代表a[1][0]的地址,2016
(7)a[2]+1 ,代表a[2][0]的地址的下一位,即a[2][1]的地址,2000+4*9=2036
(8)*(a+1)+1,*(a+1)为第二行的首地址即a[1][0],然后再把这个地址+1,即a[1][1]的地址,2016+4=2020
(9)*(*(a+2)+2),*(a+2)为a[2][0]的地址,*(a+2)+2为a[2][2]的地址,最外面加*,为a[2][2]的值21.