Tag: 函数

函数定义中的指针与数组:void fct1(int * p)和void fct1(int p )之间有什么区别?

我想知道它们之间有什么区别 void fct1(int *p) 和 void fct1(int p[]) 我知道这两个都是指针,但有任何差异

特定函数与许多参数与上下文相关

一个例子 假设我们有一个要写的文本,可以转换为“大写或小写”,并可以“​​左,中,右”打印。 具体案例实现(function太多) writeInUpperCaseAndCentered(char *str){//..} writeInLowerCaseAndCentered(char *str){//..} writeInUpperCaseAndLeft(char *str){//..} and so on… VS 许多Argument函数(糟糕的可读性,甚至在没有很好的自动完成IDE的情况下很难编写代码) write( char *str , int toUpper, int centered ){//..} VS 依赖于上下文(难以重用,难以编码,使用丑陋的全局变量,有时甚至无法“检测”上下文) writeComplex (char *str) { // analize str and perhaps some global variables and // (under who knows what rules) put it center/left/right and upper/lowercase } 也许还有其他选择..(并且欢迎) 问题是: 这个(经常性的)三难问题是否有任何良好的实践或经验/学术建议 ? 编辑: […]

如何从C中的函数返回字符数组?

甚至可能吗? 假设我想返回一个包含两个字符的数组 char arr[2]; arr[0] = ‘c’; arr[1] = ‘a’; 来自一个function。 我甚至用什么类型的function? 我唯一的选择是使用指针并使函数无效吗? 到目前为止,我已经尝试过使用char *函数或char []。 显然你只能有char(* [])的function。 我想避免使用指针的唯一原因是函数必须在遇到“返回内容”时结束。 因为“something”的值是一个字符数组(不是字符串!),它可能会根据我通过main函数传递给函数的值来改变大小。 感谢提前回复的任何人。

计算字符串中的单词 – c编程

我需要编写一个函数来计算字符串中的单词。 出于此赋值的目的,“单词”被定义为非空的非空白字符序列,通过空格与其他单词分隔。 这是我到目前为止: int words(const char sentence[ ]); int i, length=0, count=0, last=0; length= strlen(sentence); for (i=0, i<length, i++) if (sentence[i] != ' ') if (last=0) count++; else last=1; else last=0; return count; 我不确定它是否有效,因为我无法测试它直到我的整个程序完成并且我不确定它是否会起作用,是否有更好的方法来编写这个函数?

将指针的2D数组传递给函数C.

我有一个2D数组指针 main.c Int32 * pRxSeqAddr[2][2]; func(pRxSeqAddr); / func.c void func( Int32** pRxSeqAddrPtr) { /// } 我收到此错误: argument of type “Int32 *(*)[2]” is incompatible with parameter of type “Int32 ** 我知道如果它是指针的1D数组那么这个函数调用很好,但是2D唠叨我… 请帮忙

具有旧样式函数的C编译器的行为,没有原型

当我的程序包含两个文件时: main.c中 #include int main(void) { printf(“%lf\n”,f()); return 0; } func.c double f(int a) { return 1; } 编译器不显示任何错误。 当我的程序只包含一个文件时: main.c中 #include int main(void) { printf(“%lf\n”,f()); return 0; } double f(int a) { return 1; } Visual C ++ 2008编译器显示以下错误: Error 2 error C2371: ‘f’ : redefinition; different basic types d:\temp\projects\function1\function1\1.c 8 function1 任何人都能解释这种奇怪的行为吗?

array 括号内的static关键字

我最近遇到了static关键字的新用法。 static意味着什么? void fun(int some_array[static 7]); 编辑 :有人可以举例说明这有用吗?

c指针,如何将它/它们释放到一个函数中

这是我的代码: #include #include #include void getinfo(unsigned int a, unsigned int b, char **pStr); int main(){ unsigned int len_max = 8; unsigned int current_size = 0; current_size = len_max; char *host, *user; char *pStr = malloc(len_max); if(pStr == NULL){ perror(“\nMemory allocation\n”); return EXIT_FAILURE; } printf(“Inserisci hostname: “); getinfo(len_max, current_size, &pStr); if((host=malloc(strlen(pStr)+1 * sizeof(char))) == NULL) abort(); […]

究竟什么是递归并解释这个程序的输出?

我真的无法理解这段代码。 当一个函数调用自己真正发生的事情时? 我知道它与堆栈的概念有关,但我仍然无法解决这些问题。 #include fun(int); main() { int x=3; fun(x); } fun(int a) { if(a<0) { fun(–a); // what happens when function calls itself printf("%d",a); fun(–a); } } 请解释在此过程中发生的事件顺序。

C:未指定数量的参数 – void foo()

我在这里读到,在C void foo()表示a function foo taking an unspecified number of arguments of unspecified type 。 任何人都可以给我或指向一个C函数采用未指定数量的参数的示例吗? 在C可以应用什么? 我在网上找不到任何东西。 谢谢!