帮我理解code的这短短块 [英] Help me understand this short chunk of code

查看:100
本文介绍了帮我理解code的这短短块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我明白这个code计算变量的参数的个数之和,不过,我不明白它是如何工作的。它看起来非常抽象的我。有人可以解释如何下面的作品?

谢谢!

 的#include<&stdio.h中GT;#定义和(...)\\
    _sum(sizeof的((INT []){} __VA_ARGS__)/的sizeof(INT),(INT []){} __VA_ARGS__)INT _sum(为size_t计数,int类型[])
{
    INT S = 0;
    而(count--)S + =值[统计]
    返回S;
}INT主要(无效)
{
    的printf(%i的,和(1,2,3));
}


解决方案

随着pre处理器的宏

 的#define总和(...)\\
    _sum(sizeof的((INT []){} __VA_ARGS__)/的sizeof(INT),(INT []){} __VA_ARGS__)

被称为与和(1,2,3),该行被转换(一个简单的字符串替换,替换__ __ VA_ARGS1,2,3)为:

  _sum(sizeof的((INT []){1,2,3})/的sizeof(INT),(INT []){1,2,3})

这是一个函数调用 _sum()通过两件事情:


  • 整数数组{1,2,3}这是3号(它由一个整数的大小除以三整数数组的大小获取此)。

  • 的指针数组本身(或含有相同的值,这取决于你的编译器是如何聪明的是一个完全不同的阵列)。

所有 _sum()函数并为每个整数添加到取值(这是最初为零)直到计数耗尽。


这是第一点上面负有一定的解释。当你定义的 N 元素的数组如下:

  Af  -  Ag型X [22];

数组的大小的是的sizeof(X),所有元素的大小。该数组的单个元素的大小是的sizeof(X [0]),第一个元素的大小,虽然我经常preFER在的sizeof(* X)变体。

所以,计数元件的数量,则简单地通过一个元件的尺寸划分的总大小,使用下列之一:

 的sizeof(X)/ sizeof的(X [0])
的sizeof(X)/ sizeof的(* X)

和,因为你已经问了code的详细分析,我们开始吧:

  //为所需的printf()。#包括LT&;&stdio.h中GT;//宏转换总和(N1,N2,...,NN)来_sum(N,N1,N2,...,NN)。
//这由大小除以它的大小来计算所述阵列的长度
一个int //并通过对新通过的长度和数组
//功能。
// __VA_ARGS__替换整个marcro参数列表,1,2,3中
// 这个案例。#定义和(...)\\
    _sum(sizeof的((INT []){} __VA_ARGS__)/的sizeof(INT),(INT []){} __VA_ARGS__)//函数取大小和指针int数组,并返回总和。INT _sum(为size_t计数,int类型[]){
    INT S = 0; //零初始总和。
    而(count--)//直到耗尽元素(倒计时)。
        S + =值[统计] //添加每个数组元素到累加器。
    返回S; //返回总和。
}诠释主要(无效){
    的printf(%i的,和(1,2,3)); //与1,2和3(应打印6)进行测试。
}

I understand this code calculates the sum of the args of a variable, however, I don't understand how it works. It looks really abstract to me. Can someone explain how the below works?

Thanks!

#include <stdio.h>

#define sum(...) \
    _sum(sizeof((int []){ __VA_ARGS__ }) / sizeof(int), (int []){ __VA_ARGS__ })

int _sum(size_t count, int values[])
{
    int s = 0;
    while(count--) s += values[count];
    return s;
}

int main(void)
{
    printf("%i", sum(1, 2, 3));
}

解决方案

With the pre-processor macro

#define sum(...) \
    _sum(sizeof((int []){ __VA_ARGS__ }) / sizeof(int), (int []){ __VA_ARGS__ })

being called with sum(1,2,3), the line is translated (a simple string substitution, replacing "__VA_ARGS__" with "1,2,3") into:

_sum(sizeof((int []){1,2,3}) / sizeof(int), (int []){1,2,3})

which is a function call to _sum() passing two things:

  • the number of integers in the array {1,2,3} which is 3 (it gets this by dividing the size of the three-integer array by the size of a single integer).
  • the pointer to the array itself (or a totally different array containing the same values, depending on how smart your compiler is).

All the _sum() function does is add each of the integers to s (which is initially zero) until the count runs out.


That first bullet point above bears some explanation. When you have an array of N elements defined as follows:

tType x[22];

the size of the array is sizeof(x), the size of all elements. The size of a single element of that array is sizeof(x[0]), the size of the first element, although I often prefer the sizeof(*x) variant.

So, to count the number of elements, you simply divide the total size by the size of an element, using one of the following:

sizeof(x) / sizeof(x[0])
sizeof(x) / sizeof(*x)

And, since you've asked for a detailed analysis of the code, here we go:

// Needed for printf().

#include <stdio.h>

// Macro to convert sum(n1,n2,...,nN) to _sum(N,n1,n2,...,nN).
// This calculates the length of the array by dividing its size by the size
//   of an int and passes both the length and array through to the new
//   function.
// __VA_ARGS__ is replaced with the entire marcro argument list, '1,2,3' in
//   this case.

#define sum(...) \
    _sum(sizeof((int []){ __VA_ARGS__ }) / sizeof(int), (int []){ __VA_ARGS__ })

// Function to take size and pointer to int array, and return sum.

int _sum (size_t count, int values[]) {
    int s = 0;                // Initial sum of zero.
    while(count--)            // Until elements exhausted (count down).
        s += values[count];   // Add each array element to accumulator.
    return s;                 // Return sum.
}

int main (void) {
    printf ("%i", sum(1, 2, 3));   // Test it with 1, 2 and 3 (should print 6).
}

这篇关于帮我理解code的这短短块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆