码迷,mamicode.com
首页 > 其他好文 > 详细

C lang: Compound literal

时间:2019-11-26 19:06:06      阅读:76      评论:0      收藏:0      [点我收藏+]

标签:type   add   main   value   输出   val   int   r++   printf   

Xx_Introduction

  • C99 stantard.
  • Upate array and struct a compound literal.
  • Literal is date type value.In addition to symbolic constant a constant.

    Ax_Code

#include<stdio.h>
#define COLS 4
int sum2d(const int ar[][COLS], int rows);
int sum  (const int ar[], int n);
int main(void)
{
    int total1, total2, total3;
    int *pt1; 
    int (*pt2)[COLS];

    pt1 = (int[]){10, 20};
    pt2 = (int[][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);    //compound literal

    //输出三个“东西”
    printf("total1 = %d\n", total1);
    printf("total2 = %d\n", total2);
    printf("total3 = %d\n", total3);


    return 0;
}


int sum(const int ar [], int n)
{
    int i;
    int total = 0;

    for (i = 0; i < n; i++)
        total += ar[i];

    return total;
}

int sum2d(const 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 = tot + ar[r][c];
    return tot;
}

total1 = 30
total2 = 4
total3 = 27

C lang: Compound literal

标签:type   add   main   value   输出   val   int   r++   printf   

原文地址:https://www.cnblogs.com/enomothem/p/11937563.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!