Tag: 函数

将矩阵传递给函数,C

环顾四周后,我已经构建了一个接受矩阵并执行我需要的任何function的函数,如下所示: float energycalc(float J, int **m, int row, int col){ … } 在main中,定义并填充了数组的大小,但是我不能将它传递给函数本身: int matrix[row][col]; … E=energycalc(J, matrix, row, col); 这会在编译期间产生警告 “project.c:149:警告:从不兼容的指针类型project.c传递’energycalc’的参数2:53:注意:期望’int **’但参数的类型为’int(*)[(long unsigned int) (col + -0x00000000000000001)]’ 并导致分段错误。 非常感谢任何帮助,谢谢。

如何自动将K&R函数声明转换为ANSI函数声明?

// K&R syntax int foo(a, p) int a; char *p; { return 0; } // ANSI syntax int foo(int a, char *p) { return 0; } 如您所见,在K&R样式中,变量的类型在新行中而不是在大括号中声明。 如何自动将K&R函数声明转换为ANSI函数声明? 在Linux中有人知道这么容易使用的工具吗?

C将int数组指针作为参数传递给函数

我想将B int数组指针传递给func函数,并能够从那里更改它,然后查看main函数中的更改 #include int func(int *B[10]){ } int main(void){ int *B[10]; func(&B); return 0; } 上面的代码给了我一些错误: In function ‘main’:| warning: passing argument 1 of ‘func’ from incompatible pointer type [enabled by default]| note: expected ‘int **’ but argument is of type ‘int * (*)[10]’| 编辑:新代码: #include int func(int *B){ *B[0] = 5; } int main(void){ […]

在较大的字符串中查找子字符串的位置

我创建了一个函数,该函数应该在较大的字符串中找到子字符串的第一个字符的数字位置。 我输出有一些问题,我不太清楚为什么。 这些问题包括每次返回-1而不是子串的整数位置。 我已经调试过,无法追踪function出错的地方。 这是函数应该执行的方式:如果我的字符串是“狗很快”而我正在搜索子字符串“dog”,则该函数应返回4.感谢chqrlie对循环的帮助。 这是function: int findSubString(char original[], char toFind[]) { size_t i, j; int originalLength = 0; int toFindLength = 0; originalLength = strlen(original) + 1; toFindLength = strlen(toFind) + 1; for (i = 0; i < toFindLength + 1; i++) { for (j = 0; j < originalLength + 1; j++) { […]

何时在C中使用类似函数的宏

今晚我正在阅读一些用C语言编写的代码,文件顶部是类似函数的宏HASH: #define HASH(fp) (((unsigned long)fp)%NHASH) 这让我想知道,为什么有人会选择使用类似函数的宏来实现这种函数,而不是将它作为常规的vanilla C函数实现? 每种实施的优缺点是什么? 谢谢你!

C语言中参数的默认值和C中的函数重载

将C ++库转换为ANSI C,似乎ANSI C不支持函数变量的默认值,或者我错了? 我想要的是类似的东西 int funcName(int foo, bar* = NULL); 另外,ANSI C中的函数重载是否可行? 需要 const char* foo_property(foo_t* /* this */, int /* property_number*/); const char* foo_property(foo_t* /* this */, const char* /* key */, int /* iter */); 当然可以用不同的方式命名它们但是习惯于C ++我曾经用于函数重载。

将函数声明为“内联”的好处?

每当我读到C中的“内联”声明时,都会提到它只是编译器的一个提示 (即它不必遵守它)。 那么添加它有什么好处,还是我应该依赖编译器比我更了解?

如何在C中定义函数数组

我有一个包含这样的声明的结构: void (*functions[256])(void) //Array of 256 functions without arguments and return value 在另一个函数我想定义它,但有256个函数! 我可以这样做: struct.functions[0] = function0; struct.functions[1] = function1; struct.functions[2] = function2; 等等,但这太累了,我的问题是有办法做这样的事吗? struct.functions = { function0, function1, function2, function3, …, }; 编辑 :Chris Lutz所说的修正了语法错误。

存储Luafunction?

从C 调用Lua函数是相当简单的,但有没有办法将 Lua函数存储在某处供以后使用? 我想存储传递给我的C函数的用户定义的Lua函数以用于事件,类似于Connect函数在wxLua中的工作方式 。

使用C中的函数更改数组?

我想调用一个函数,我希望该函数将程序中字符串或数组的内容更改为常量。 伪代码: some_array = “hello” print some_array #prints “hello” changeArray(some_array) print some_array #prints “bingo” 我知道我必须将指针传递给该函数。 这是我写的, void changeArray(char *arr){ arr = “bingo”; } int main(int argc, const char* argv[]){ char *blah = “hello”; printf(“Array is %s\n”,blah); changeArray(blah); printf(“Array is %s\n”,blah); return EXIT_SUCCESS; } 我怎样才能做到这一点?