#include
int main()
{
int *ptr;
int torf[2][2] = {12, 14, 16};
ptr = torf[0];//指向torf[0][0]
return 0;
}
int torf[2][2] = {12, {14, 16}};
int torf[2][2] = {{12}, {14, 16}};
#include
int main()
{
int *ptr;
int torf[2][2] = {{12}, {14, 16}};
ptr = torf[0];
printf("*ptr=%d, *(ptr+2)=%d\n", *ptr, *(ptr+2));
return 0;
}
*ptr=12, *(ptr+2)=14
1,2不重要,重要的是3中易错,ref+1指向4,但是++ref不是有效表达式!!!不可以改变数组名的地址,人家是常量,改不了
不明白??????为啥取址就是一种表示??我以为题目是让我们用指针表示法表示
我的答案
#include
int main()
{
int torf[3][2] = {{12}, {14, 16},{2, 6}};
printf("torf[2][1] = %d, *(*(torf+2)+1) = %d\n", torf[2][1], *(*(torf+2)+1));
return 0;
}
torf[2][1] = 6, *(*(torf+2)+1) = 6
a. *(*(grid+22)+56)//grid是指向第一个子数组,grid+22指向第23个子数组,即grid[22],
*(grid+22)等于grid[22],grid[22]是一个内含100个元素的数组的首元素地址,grid[22]+56则是指向第57个元素
b1.*grid[22]//grid[22]就是第23个数的首元素grid[22][0]的地址
b2. *(grid+22)
c1.**grid
c2.*grid[0]
c3.can't think of anything····
int *psa[20];//相当于int *(psa[20]), []优先级高于*,所以psa先和[20]结合,说明psa是个数组
char (*pstr)[20];//它是指针,所以圆括号括起来使得先和*结合
int a[100] = {[99]=-1};
int b[100] = {[5]=101, [10]=101, [11]=101, [12]=101, [3]=101};
int b[100] = {[5]=101, [10]=101, 101, 101, [3]=101};//简写方式
#include
int main()
{
int a[100] = {[99]=-1};
int b[100] = {[5]=101, [10]=101, 101, 101, [3]=101};
printf("a[99]=%d,b[5]=%d, b[10]=%d, b[11]=%d, b[12]=%d, b[3]=%d\n",
a[99], b[5], b[10], b[11], b[12], b[3]);
return 0;
}
a[99]=-1,b[5]=101, b[10]=101, b[11]=101, b[12]=101, b[3]=101
#include
int main()
{
int a[100];
scanf("%d", &a);
printf("a[0]=%d\n", a[0]);
return 0;
}
22
a[0]=22
show((double [4]) {8, 3, 9, 2}, 4);
show2((double [][3]) {{8, 3, 9},{5, 4, 1}}, 2);
这次改用指针表示法重写还发现了之前的一个错误,之前写的*(*(rainfall+i)+j)!!!不对哦
而且之前求月均还除的是月!!应该除以年
#include
#define YEAR 3
#define MONTH 4
int main()
{
float sum_year[YEAR] = {}, sum_month[MONTH]={};
float total=0;
float rainfall[YEAR][MONTH] = {{3.4, 4.5, 4.2, 5.2},{4.6, 6.4, 7.3, 4.8}, {2.1, 3.1, 8.2, 5.2}};
printf("Year Rainfall(inches)\n");
for(int i=0;i<YEAR;i++)
{
for(int j=0;j<MONTH;j++)
*(sum_year+i) += *(*(rainfall+i)+j);
printf("%d: %.2f\n", 2011+i, *(sum_year+i));
total += *(sum_year+i);
}
printf("The yearly average is %.2f inches.\n\n", total/YEAR);
printf("Monthly averages:\n");
printf("Jan Feb Mar Apr\n");
for(int i=0;i<MONTH;i++)
{
for(int j=0;j<YEAR;j++)
*(sum_month+i) += *(*(rainfall+j)+i);//这次改用指针表示法重写还发现了之前的一个错误,之前写的*(*(rainfall+i)+j)!!!不对哦
printf("%.2f ", *(sum_month+i)/YEAR);//之前除的MONTH,不对哦
}
return 0;
}
2题, 2-5的代码都是我重写了一遍的,刚才浏览器突然卡了,然后就啥都没了,气哭了,现在又耐着性子重写,不过我发现重写一遍也好,我发现我还是出了两次错,就算巩固了
对于原数组,我用了const保护,注意了搭配同款const指针,0警告
听说华为编程规范要求就算if, for, while只有一个语句也要用花括号,所以就用了
听说规范不允许在while条件里写n–>0这种略显复杂的表达式,要老老实实写在花括号里面,所以也就听话了,向规范低头
#include
void copy_arr(double t[], const double s[], int n);
void copy_ptr(double *t, const double *s, int n);
void copy_ptrs(double *t, const double *s, double *tail);
void show_arr(const double *arr, int n);
int main()
{
double source[5] = {1.1, 2.2, 3.3, 4.4, 5.5};
double target1[5], target2[5], target3[5];
copy_arr(target1, source, 5);
copy_ptr(target2, source, 5);
copy_ptrs(target3, source, source+5);
printf("source: ");
show_arr(source, 5);
printf("target1: ");
show_arr(target1, 5);
printf("target2: ");
show_arr(target2, 5);
printf("target3: ");
show_arr(target3, 5);
return 0;
}
void copy_arr(double t[], const double s[], int n)
{
int i;
for(i=0;i<n;i++)
{
t[i] = s[i];
}
}
void copy_ptr(double *t, const double *s, int n)
{
double *ptr_t = t;
const double *ptr_s = s;
int i;
for(i=0;i<n;i++)
{
*ptr_t++ = *ptr_s++;
}
}
void copy_ptrs(double *t, const double *s, double * tail)
{
int d = tail - s;//指针减法,得到数组元素个数
double *ptr_t = t;
const double *ptr_s = s;
while(d>0)
{
*ptr_t++ = *ptr_s++;
d--;
}
}
void show_arr(const double *arr, int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%.2f ", arr[i]);
}
putchar('\n');
}
source: 1.10 2.20 3.30 4.40 5.50
target1: 1.10 2.20 3.30 4.40 5.50
target2: 1.10 2.20 3.30 4.40 5.50
target3: 1.10 2.20 3.30 4.40 5.50
3题
#include
void arr_max(const int *arr, int n);
int main()
{
int arr[] = {1, 12, 73, 54, 5};
arr_max(arr, 5);
return 0;
}
void arr_max(const int *arr, int n)
{
int m = *arr;
const int *ptr = arr;
while(--n>0 && *ptr>=m)
{
m = *ptr++;
}
printf("maximum: %d", m);
}
maximum: 73
4题
#include
void arr_max(const double *arr, int n);
int main()
{
double arr[] = {1.1, 10.3, 3.6, 7.3, 5.9};
arr_max(arr, 5);
return 0;
}
void arr_max(const double *arr, int n)
{
double m = *arr;
int sub=0, n1=n;
const double *ptr = arr;
while(--n>0 && *ptr>=m)
{
m = *ptr++;
sub = n1-n-1;
}
printf("maximum: %.1f\nsubscript: %d\n", m, sub);
}
maximum: 10.3
subscript: 1
5题
#include
void diff(const double *arr, int n);
int main()
{
const double arr[]={3.1, 78.2, 9.2, 7.1, 0.9};
diff(arr, sizeof arr / sizeof arr[0]);
return 0;
}
void diff(const double * arr, int n)
{
double max, min, d;
int i;
max = min = *arr;//多重赋值
const double *ptr = arr;
for(i=0;i<n;i++)
{
if(*(ptr+i)>max)
max = *(ptr+i);
else if(arr[i]<min)
min = arr[i];
}
d = max - min;
printf("maximum:%.2f\nminimum:%.2f\ndifference: %.2f\n", max, min, d);
}
maximum:78.20
minimum:0.90
difference: 77.30
6
#include
void reverse(double *ar, int n);
int main()
{
double lazy[] = {10.2, 3.4, 6.3, 76.1, 0.6};
reverse(lazy, 5);
return 0;
}
void reverse(double *ar, int n)
{
double *ptr=ar+n;
while(n>0)
{
printf("%.1f ", *--ptr);
n--;
}
}
0.6 76.1 6.3 3.4 10.2
10
#include
#define SIZE 4
void add_arr(int *ar1, int *ar2, int *ar3, int n);
int main()
{
int apple[SIZE] = {1, 2, 3, 4};
int pear[SIZE] = {4, 3, 2, 1};
int sum[SIZE] = {};
add_arr(apple, pear, sum, SIZE);
return 0;
}
void add_arr(int *ar1, int *ar2, int *ar3, int n)
{
int i;
int *ptr3=ar3, *ptr1=ar1, *ptr2=ar2;
for(i=0;i<n;i++)
{
*(ptr3+i) = *(ptr1+i) + *(ptr2+i);
printf("%d ", *(ptr3+i));
}
}
5 5 5 5