是否存在可以将 in 语句作为参数的等效函数?
Is there a function equivalent that can take in statements as arguments?
我有以下一段代码,我需要在整个程序中使用大约 5 次,但用不同的代码行代替注释。
while (loop_day < (day+1)) {
while (loop_size < (size+1)) {
//new lines here
size = size + 1;
}
loop_day = loop_day + 1;
}
我可以多次复制粘贴,但出于审美原因,我真的不想这样做。我尝试搜索 "functions that could take in statements as arguments",但没有找到合适的内容。
编辑:我想"embed"各种语句到代码中。
一个例子:
while (loop_day < (day+1)) {
while (loop_size < (size+1)) {
// code that stores various values into an array
size = size + 1;
}
loop_day = loop_day + 1;
}
while (loop_day < (day+1)) {
while (loop_size < (size+1)) {
// code that reads values stored in that array
size = size + 1;
}
loop_day = loop_day + 1;
}
但我想要像这样的东西:
custom_loop {
// code that stores various values into an array
}
custom_loop {
// code that reads values stored in that array
}
你可以想到回调函数。例如,
typedef void (*t_func)(int, int);
void doLoopOverDaysAndSize(t_func callback)
{
while (loop_day < (day+1)) {
while (loop_size < (size+1)) {
callback(loop_day, loop_size)
size = size + 1;
}
loop_day = loop_day + 1;
}
}
然后你可以像这样传递一些函数
void myDaySizeHandler(int day, int size)
{
// do something
}
人们往往会忘记 include 可以在代码中的任何地方使用,而不仅仅是对 headers ( Including one C source file in another?) 有用。然而,有些人不喜欢他们。
示例:
common.inc:
x = x + 1;
main.c
int main()
{
{
int x = 3;
#include "common.inc"
printf("x = %d\n", x);
}
{
double x = 1.234;
#include "common.inc"
printf("x = %f\n", x);
}
return 0;
}
-编辑-
对于您的代码,这将导致:
commonStart.inc
while (loop_day < (day+1))
{
while (loop_size < (size+1))
{
commonEnd.inc
size = size + 1;
}
loop_day = loop_day + 1;
}
main.c
#include "commonStart.inc"
//new lines here
#include "commonEnd.inc"
我有以下一段代码,我需要在整个程序中使用大约 5 次,但用不同的代码行代替注释。
while (loop_day < (day+1)) {
while (loop_size < (size+1)) {
//new lines here
size = size + 1;
}
loop_day = loop_day + 1;
}
我可以多次复制粘贴,但出于审美原因,我真的不想这样做。我尝试搜索 "functions that could take in statements as arguments",但没有找到合适的内容。
编辑:我想"embed"各种语句到代码中。
一个例子:
while (loop_day < (day+1)) {
while (loop_size < (size+1)) {
// code that stores various values into an array
size = size + 1;
}
loop_day = loop_day + 1;
}
while (loop_day < (day+1)) {
while (loop_size < (size+1)) {
// code that reads values stored in that array
size = size + 1;
}
loop_day = loop_day + 1;
}
但我想要像这样的东西:
custom_loop {
// code that stores various values into an array
}
custom_loop {
// code that reads values stored in that array
}
你可以想到回调函数。例如,
typedef void (*t_func)(int, int);
void doLoopOverDaysAndSize(t_func callback)
{
while (loop_day < (day+1)) {
while (loop_size < (size+1)) {
callback(loop_day, loop_size)
size = size + 1;
}
loop_day = loop_day + 1;
}
}
然后你可以像这样传递一些函数
void myDaySizeHandler(int day, int size)
{
// do something
}
人们往往会忘记 include 可以在代码中的任何地方使用,而不仅仅是对 headers ( Including one C source file in another?) 有用。然而,有些人不喜欢他们。
示例:
common.inc:
x = x + 1;
main.c
int main()
{
{
int x = 3;
#include "common.inc"
printf("x = %d\n", x);
}
{
double x = 1.234;
#include "common.inc"
printf("x = %f\n", x);
}
return 0;
}
-编辑- 对于您的代码,这将导致:
commonStart.inc
while (loop_day < (day+1))
{
while (loop_size < (size+1))
{
commonEnd.inc
size = size + 1;
}
loop_day = loop_day + 1;
}
main.c
#include "commonStart.inc"
//new lines here
#include "commonEnd.inc"