(C语言)数组与指针的使用

本文通过几个实例展示了如何使用C语言中的数组与指针进行数据复制、显示及查找最大值的操作,并涉及二维数组的处理方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 1None.gif#include <stdio.h>
 2None.gif#define SIZE 5
 3None.gifvoid CopybyArray(double ar[],double target[],int);
 4None.gifvoid CopybyPoint(double *ar,double *target,int);
 5None.gifvoid DisplayNum(double *array,int);
 6None.gifdouble MaxNum(double *array,int);
 7None.gifint main(void)
 8ExpandedBlockStart.gifContractedBlock.gifdot.gif{
 9ExpandedSubBlockStart.gifContractedSubBlock.gif    double source[SIZE]=dot.gif{1.2,3.2,4.5,6.3,8.2};
10InBlock.gif    double target1[SIZE],target2[SIZE];
11InBlock.gif    double max;
12InBlock.gif    printf("The numbers in source:");
13InBlock.gif    DisplayNum(source,SIZE);
14InBlock.gif    CopybyArray(source,target1,SIZE);
15InBlock.gif    CopybyPoint(source,target2,SIZE);
16InBlock.gif    printf("\nAfter copy the numbers:");
17InBlock.gif    printf("\ntarget1:");
18InBlock.gif    DisplayNum(target1,SIZE);
19InBlock.gif    printf("\ntarget2:");
20InBlock.gif    DisplayNum(target2,SIZE);
21InBlock.gif
22InBlock.gif    max=MaxNum(source,SIZE);
23InBlock.gif    printf("max is %f.",max);
24InBlock.gif    //two
25ExpandedSubBlockStart.gifContractedSubBlock.gif    double source2[3][4]=dot.gif{
26ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{2,5,8.9,3.6},
27ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{4.5,6.3,12,36},
28ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{5.5,8.1,9,98}
29ExpandedSubBlockEnd.gif        }
;
30InBlock.gif    double target3[3][4];
31InBlock.gif    printf("\nbefore copy,source2:\n");
32InBlock.gif    for(int n=0;n<3;n++)
33ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
34InBlock.gif            DisplayNum(source2[n],4);
35InBlock.gif            printf("\n");
36ExpandedSubBlockEnd.gif        }

37InBlock.gif    for(int i=0;i<3;i++)
38InBlock.gif        CopybyPoint(source2[i],target3[i],4);
39InBlock.gif    printf("\ntarget3:\n");
40InBlock.gif    for(int m=0;m<3;m++)
41ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
42InBlock.gif            DisplayNum(target3[m],4);
43InBlock.gif            printf("\n");
44ExpandedSubBlockEnd.gif        }

45InBlock.gif    //choice
46ExpandedSubBlockStart.gifContractedSubBlock.gif    double source3[7]=dot.gif{2.3,5.6,8,9,5.8,89,12};
47InBlock.gif    double target4[3];
48InBlock.gif    printf("\nsource3:\n");
49InBlock.gif    DisplayNum(source3,7);
50InBlock.gif    CopybyPoint(&source3[2],target4,3);
51InBlock.gif    printf("\ntarget4:");
52InBlock.gif    DisplayNum(target4,3);
53InBlock.gif    return 0;
54ExpandedBlockEnd.gif}

55None.gifvoid CopybyArray(double ar[],double target[],int size)
56ExpandedBlockStart.gifContractedBlock.gifdot.gif{
57InBlock.gif    for(int i=0;i<size;i++)
58InBlock.gif        target[i]=ar[i];
59InBlock.gif    return ;
60ExpandedBlockEnd.gif}

61None.gifvoid CopybyPoint(double * ar,double * target,int size)
62ExpandedBlockStart.gifContractedBlock.gifdot.gif{
63InBlock.gif    for(int i=0;i<size;i++)
64ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
65InBlock.gif            *target=*ar++;
66InBlock.gif            target++;
67ExpandedSubBlockEnd.gif        }

68InBlock.gif    return ;
69ExpandedBlockEnd.gif}

70None.gifvoid DisplayNum(double *array,int size)
71ExpandedBlockStart.gifContractedBlock.gifdot.gif{
72InBlock.gif    for(int i=0;i<size;i++)
73InBlock.gif        printf("%f\t",*array++);
74ExpandedBlockEnd.gif}

75None.gifdouble MaxNum(double *array,int size)
76ExpandedBlockStart.gifContractedBlock.gifdot.gif{
77InBlock.gif    double max;
78InBlock.gif    max=0.0;
79InBlock.gif    int p;
80InBlock.gif    for(int i=0;i<size;i++)
81ExpandedSubBlockStart.gifContractedSubBlock.gif        dot.gif{
82InBlock.gif            if(*array>=max)
83ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
84InBlock.gif                    max=*array++;
85InBlock.gif                    p=i;
86ExpandedSubBlockEnd.gif                }

87ExpandedSubBlockEnd.gif        }

88InBlock.gif        printf("number %d\n",p);
89InBlock.gif    return max;
90ExpandedBlockEnd.gif}

91None.gif

转载于:https://www.cnblogs.com/feiren1421/archive/2006/02/27/338371.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值