美文网首页
第十章 数组和指针

第十章 数组和指针

作者: 小风xf | 来源:发表于2018-11-30 20:04 被阅读0次

    按月打印天数

    编译器 计算元素的 个数

    #include <stdio.h>

    #include <stdlib.h>

    #define MONTHS 12

    void main()

    const int days[] = {31,28,31,30 ,31,30,31,31,30,31,30,31};

    int i ;

    for ( i = 0; i < sizeof (days)/sizeof(days[0]) ; i++)

    {

    printf(" months = %d dDAY = %d\n",i+1,days[i]);

    }

    printf("size of (day ) = %d .size of (day[0]= %d",sizeof (days),sizeof(days[0]));

    system("pause");

    }

    指针加法

    #include <stdio.h>

    #include <stdlib.h>

    #define SIZE 4

    main()

    {  short dates[SIZE];

    short *pti;

    short index;

    double bills[SIZE];

    double *ptf;

    pti = dates;

    ptf = bills;

    printf("%23s %10s \n","short " ,"double");

    for ( index = 0; index < SIZE; index ++)

    {

    printf("%d  %10p %10p \n",index,pti+index,ptf+index);

    }

    system("pause");

    }

    使用指针符号 

    #include <stdio.h>

    #include <stdlib.h>

    #define  MONTHS 12

    main()

    int days[] = {31,28,31,30 ,31,30,31,31,30,31,30,31};

    int i ;

    for ( i = 0; i <  MONTHS ; i++)

    {

    printf(" months = %2d dDAY = %5d\n",i+1,*(days+i));

    }

    system("pause");

    }

    对一个元素的 所有元素求和 

    #include <stdio.h>

    #include <stdlib.h>

    #define  MONTHS 12

    int sum (int * , int );

    main()

    int days[] = {31,28,31,30 ,31,30,31,31,30,31,30,31};

    long answer;

    answer = sum (days,MONTHS);

    printf("answer = %d\n",answer);

    printf("sizeof days = %d\n",sizeof days);

    system("pause");

    }

    int sum  (  int * ar , int n)

    {

    int i,  total = 0;

    for (  i = 0; i < n; i++)

    {

    total += *(ar+i);

    }

    printf("sizeof %d",sizeof ar );

    return total;

    }

    使用指针参数 

    ‘#include <stdio.h>

    #include <stdlib.h>

    #define  MONTHS 12

    int sum (int * , int * );

    main()

    int days[] = {31,28,31,30 ,31,30,31,31,30,31,31,31};

    long answer;

    answer = sum (days,days+ MONTHS);

    printf("answer = %d\n",answer);

    printf("sizeof days = %d\n",sizeof days);

    system("pause");

    }

    int sum  (  int * ar , int* n)

    {

    int  total = 0;

    while (ar < n)

    {

    total += *ar;

    ar++;

    }

    printf("sizeof %d",sizeof ar );

    return total;

    }

    指针运算的 优先3  级 

    #include <stdio.h>

    #include <stdlib.h>

    #define  MONTHS 12

    int data [2] = { 100, 200};

    int moredata[2] = { 300,400};

    void main()

    int *p1, *p2 ,*p3;

    p1 = p2 = data;

    p3 = moredata;

    printf("*p1 = %d  *p2 = %d *p3 = %d \n", *p1 ,*p2 ,*p3);

      printf("*p1++ = %d    *++p2 = %d (*p3)++ = %d \n", *p1++ ,*++p2 ,(*p3)++);

      printf("*p1 = %d  *p2 = %d *p3 = %d \n", *p1 ,*p2 ,*p3);

    system("pause");

    }

    指针操作 

    #include <stdio.h>

    #include <stdlib.h>

    void main()

    int nrn[5] = {100,200,300,400,500};

    int * prt1 ,* prt2 ,* prt3;

    prt1 = nrn;

    prt2 = nrn+2;

    printf (" \n adding an int to a pointer ;|\n");

    printf("ptr1 = %p  *ptr1 = %d &ptr1 = %p \n",prt1,*prt1,&prt1);

    prt3 = prt1 + 4;

    printf (" \n adding an int to a pointer ;|\n");

    printf("ptr1+4 = %p  ptr1+4 = %d *(ptr1+3) = %d  \n",prt1 +4 ,*(prt1 +4),*(prt1+3));

    prt1++;

    printf("\n valuess afrerptr1 ++ \n");

    printf("ptr1 = %p  *ptr1 = %d &ptr1 = %p \n",prt1,*prt1,&prt1);

    prt2 --;

    printf("ptr2 = %p  *ptr2 = %d &ptr2 = %p \n",prt2,*prt2,&prt2);

    --prt1;

    ++prt2;

    printf("\n valuess afrerptr1 ++ \n");

    printf("ptr1 = %p *ptr1 = %d ptr2 = %p *ptr2 = %d  \n",prt1,*prt1,prt2,*prt2);

    printf("\n valuess afrerptr1 ++ \n");

    printf("ptr2 = %p  ptr1 = %p ptr2 -ptr1 = %d  \n",prt2,prt1,prt2-prt1);

    printf("\n valuess afrerptr1 ++ \n");

    printf("ptr3 = %p *ptr3 = %d  ptr3 -2 = %p *(ptr3 -2) = %d  \n",prt3,*prt3,prt3-2,*(prt3-2));

    system("pause");

    }

    处理 数组的函数 不使用 return 机制 

    #include <stdio.h>

    #include <stdlib.h>

    #define SIZE 5

    void show_array (const double  * , int );

    void mult_array(double *,int ,double);

    void main()

    double dip[SIZE] = {20.0,17.66,8.2,15.3,22.22};

    printf("the original dip array \n");

    show_array(dip , SIZE);

    mult_array(dip, SIZE,2.5);

    printf("after\n");

    show_array(dip , SIZE);

    system("pause");

    }

    void show_array (const double  *ar , int n )

    {

    int i ;

    for ( i = 0; i < n; i++)

    {

    printf("%8.3f",*(ar+i));

    }

    putchar('\n');

    }

    void mult_array(double *ar,int n ,double mult)

    {

      int i ;

    for ( i = 0; i < n; i++)

    {

    * (ar + i) *= mult;

    }

    }

    指针和多位数组 

    #include <stdio.h>

    #include <stdlib.h>

    #define SIZE 5

    void main()

    int zippo [4][2] = {{2,4},{6,8},{1,3},{5,7} };

    printf("zippo = %p  zippo + 1 = %p  \n",zippo,zippo+1 );

    printf("zippo[0] = %p zippo[0]+1 = %p,  \n",zippo[0] ,zippo[0]+1);

    printf("*zippo  = %p *zippo +1 = %p,  \n",*zippo  ,*zippo +1);

    printf("zippo[0][0]  = %d  \n",zippo[0][0] );

    printf("*zippo [0]  = %d  \n",*(*(zippo +0)+1) );

      printf("zippo[2][1]  = %d  \n",zippo[2][1] );

      printf(" *(*(zippo+2)+1)  = %d  \n", *(*(zippo+2)+1) );

    system("pause");

    处理二位数组 的函数 

    #include <stdio.h>

    #include <stdlib.h>

    #define ROWS 3

    #define COLS  4

    void sum_rows(int ar[][COLS] ,int );

    void sum_cols(int [] [COLS],int );

    int sum2d(int (*ar) [COLS] ,int );

    void main()

    int junk[ROWS][COLS] = {

    {2,4,6,8},

    {3,5,7,9},

    {12,10,8,6}

    };

    sum_rows(junk,ROWS);

    sum_cols(junk,ROWS);

    printf ("sum of all elements %d \n ",sum2d(junk,ROWS));

    system("pause");

    void sum_rows(int ar[] [COLS] ,int rows )

    {

    int r;

    int c;

    int tot;

    for (r  = 0; r < rows; r++)

    {

    tot = 0;

    for (c = 0; c < COLS; c++)

    {

    tot += *(*(ar+r)+c);

    }

    printf("row %d : sum = %d\n" , r, tot);

    }

    }

    void sum_cols(int ar[] [COLS],int rows )

    {

    int r;

    int c;

    int tot;

    for (c  = 0; c < COLS; c++)

    {

    tot = 0;

    for (r = 0; r < rows; r++)

    {

    tot += *(*(ar+r)+c);

    }

    printf("row %d : sum = %d\n" , c, tot);

    }

    }

    int sum2d(int ar[] [COLS] ,int rows)

    {

      int r;

    int c;

    int tot = 0;

    for (r  = 0; r <rows ; r++)

    {

    for (c = 0; c < COLS;c++)

    {

    tot += *(*(ar+r)+c);

    }

    }

    return tot;

    }

    \

    边长数组vs 2012编译失败  xcode 编译成功

    #include <stdio.h>

    #include <stdlib.h>

    #define ROWS 3

    #define COLS  4

    int sum2d(int rows  ,int cols,int ar[rows][cols]);

    void main()

    int i ,j;

      int rs = 3;

    int cs = 10;

    int junk[ROWS][COLS] = {

    {2,4,6,8},

    {3,5,7,9},

    {12,10,8,6}

    };

    int morejunk[ROWS -1][COLS+2] = {

    {20,30,40,50,60,70},

    {5,6,7,8,9,10},

    };

    int varr[rs][cs];

    for ( i = 0; i < rs; i++)

    {

    for ( j = 0;j < cs; j++)

    {

    varr [i][j] = i * j +j;

    }

    }

    printf("3*5 array\n");

    printf("sum of all elements = %d \n", sum2d(ROWS,COLS,junk));

    printf("2*6 array\n");

    printf("sum of all elements = %d \n", sum2d(ROWS-1,COLS+2,morejunk));

    printf("3*10 array\n");

    printf("sum of all elements = %d \n", sum2d(rs,cs,varr));

    system("pause");

    int sum2d(int rows  ,int cols,int ar[rows][cols])

    {

      int r;

    int c;

    int tot = 0;

    for (r  = 0; r <rows ; r++)

    {

    for (c = 0; c < cols;c++)

    {

    tot += *(*(ar+r)+c);

    }

    }

    return tot;

    }

    有趣的常数

    #include <stdio.h>

    #include <stdlib.h>

    #define COLS  4

    int sum2d(int (*ar)[COLS]  ,int );

    int sum (int * ,int );

    void main()

    {

        int total1,total2,total3;

        int *pt1;

        int (*pt2)[COLS];

        pt1 = (int [2])  {10,20};

        pt2 = (int [2][COLS]) {{1,2,3,-9},{4,5,6,-8}};

        total1=sum (pt1,2);

        total2=sum2d (pt2,2);

        total3=sum ((int [] ){4,4,4,5,5,5},6);

        printf("total1 = %d \n", total1);

        printf("total2 = %d \n", total2);

        printf("total3 = %d \n", total3);

        system("pause");

    }

    int sum (int *ar,int n)

    {

        int i;

        int total = 0;

        for(i  =0; i <n ; i++)

        {

            total+= *(ar+i);

        }

        return total;

    }

    int sum2d(int (*ar)[COLS]  ,int rows)

    {

        int r;

        int c;

        int tot = 0;

        for(r  =0; r <rows ; r++)

        {

            for (c = 0; c <COLS;c++)

            {

                tot += *(*(ar+r)+c);

            }

        }

        return tot;

    }

    #include <stdio.h>

    #include <stdlib.h>

    void sign_off (void );

    void too_bad (void );

    void main()

    {

    int n ;

    atexit (sign_off);

    puts("enter an integer");

    if (scanf ("%d",&n) != 1)

    {

    puts("THAT NO integer");

    atexit(too_bad);

    exit(EXIT_FAILURE);

    }

    printf("%d is %s \n" ,n ,( n % 2 == 0) ? "even" : "odd");

    system("pause");

    }

    void sign_off()

    {

    puts (" thus terminates another agnificetnt program from");

    puts ("seesaw software ");

    }

    void too_bad()

    {

    puts (" seesaw software extends its heartfelt condolences ");

    puts ("  to you upon the failure of your program ");

    }

    相关文章

      网友评论

          本文标题:第十章 数组和指针

          本文链接:https://www.haomeiwen.com/subject/hxlkcqtx.html