Tag: 参数

动态参数传递给C中的execlp()函数

为简单起见,我修改了我的程序。 我想要做的是在运行时接受任意数量的参数并将其传递给execlp() 。 我正在使用固定长度的2d数组m[][]这样任何未使用的(剩余)槽可以作为NULL传递给execlp (在这种情况下为m[2][] )。 #include #include #include #include int main() { char m[3][5], name[25]; int i; strcpy(name, “ls”); strcpy(m[0], “-t”); strcpy(m[1], “-l”); //To make a string appear as NULL (not just as an empty string) for(i = 0; i < 5; i++) m[2][i] = '\0'; // or m[2][i] = 0 (I've tried both) […]

为什么我会尝试将’char(* c)’传递给期望’const char(* c)’的函数?

我知道char ** vs const char **事情(就像在c faq中描述的那样)但是我看不到任何使用指向数组的指针的情况会导致数组内部的一些内容被实际修改。 我的代码: void fun(const char (*p)[6]) { printf(“%s”, p[0]); } int main(int argc, char *argv[]) { char a[6] = “hello”; char (*c)[6]; c = &a; fun(c); } 使用gcc编译时给出以下输出: test.c:17:9: warning: passing argument 1 of ‘fun’ from incompatible pointer type test.c:5:10: note: expected ‘const char (*)[6]’ but argument is of […]

错误:参数的类型不兼容

我正在用C写一个列表。以下是来源: #include #include struct list { int value; struct list *next; }; typedef struct list ls; void add (ls **head, ls **tail, int val) { ls *new, *tmp1, *tmp2; if (NULL == *head) { new = (ls*)malloc(sizeof(ls)); *head = new; *tail = new; new->value = val; new->next = NULL; return; } else { tmp1 = […]

为什么有必要将列数作为函数参数传递?

当我在函数的参数中传递矩阵时,使用括号,我也需要传递列数。 为什么? #include //int function(int matrix[][5]){ //Will work int function(int matrix[][]){ //Won’t work return matrix[0][0]; } int main(){ int matrix[5][5]; matrix[0][0] = 42; printf(“%d”, function(matrix)); } gcc错误: prog.c:3:18: error: array type has incomplete element type int function(int matrix[][]){ ^ prog.c: In function ‘main’: prog.c:10:5: error: type of formal parameter 1 is incomplete printf(“%d”, function(matrix)); ^ […]

用C标记字符串?

我正在研究用C编写的计算器的终端解析器。我无法弄清楚如何连接运算符之间的所有数字以将它们放入数组中。 例如,如果输入(命令行参数)是“ 4+342 ”,则理想情况下input[] = {“4”, “+”, “342”} 。 到目前为止,这是我的代码。 我包括 , 和 。 typedef char * string; int main(int argc, char *argv[]) { string inputS = argv[1]; string input[10]; string temp; printf(“%s\n”, inputS); int i; int len = strlen(inputS); printf(“parsed:\n”); for(i = 0; i < len; inputS++, i++) { if(isdigit(*inputS)) { printf("%c",*inputS); } else […]

sizeof运算符在C中有什么参数?

[ 原标题称为’sizeof function’。 ] 我尝试了这些,他们都工作: char * p; printf(“* p的大小是%d \ n”,sizeof(* p)); // result = 1 printf(“p的大小是%d \ n”,sizeof(p)); //结果= 4 printf(“p的大小是%d \ n”,sizeof(&p)); //结果= 4 我想知道为什么第一个printf是1,第二个和第三个是4? 那么sizeof可以实际采用什么参数呢?

传递一个常数矩阵

提到这个问题,特别是litb接受的答案 ,我想知道为什么gcc抱怨这个: void func(const int (*ip)[3]) { printf(“Value: %d\n”, ip[1][1]); } int main() { int i[3][3] = { {0, 1, 2} , {3, 4, 5}, {6, 7, 8} }; func(i); return 0; } 如果我消除了const ,编译器会保持静止。 我有什么误会吗? 我想确保func不修改我的数组。 编辑:如果我为我的矩阵定义数据类型,会发生同样的事情: typedef int Array[3][3]; void func(const Array *p) { printf(“Value: %d\n”, (*p)[1][1]); } int main() { Array a […]

两个带有void和空参数列表的函数声明

我想知道为什么以下代码: void foo(void); void foo() { } 在gcc中有效。 在C中,没有重载和上面的声明(实际上,其中一个是定义)声明两个不同的函数(第一个不接受任何参数,第二个可以接受任何参数)类型)。 但是,如果我们为第一个函数提供定义: void foo(void) { } void foo() { } 由于重新定义,此次编译失败。 但是 ,第一个代码仍然是正确的,可能会令人困惑,如下所示: void foo(void); int main(void) { foo(); //OK //foo(5); //Wrong, despite ->the definition<- allows it } void foo() { } 另一方面,这样的事情是无效的: void foo(int); void foo() //error: number of arguments doesn’t match prototype { } 我认为与我的第一个前面的代码相比,编译器的行为有点奇怪。 […]

将参数传递给_beginthread() – 什么是错的?

我有这个代码,我没有得到预期的结果……什么是错的? typedef struct { int data1; int data2; }t; void foo(int a, int b) { Handle handle; t arg; arg.data1 = a; arg.data2 = b; handle = (HANDLE) _beginthread( myFunc, 0, (void*) &arg); } void myFunc(void *param) { t *args = (t*) param; int x = args->data1; int y = args->data2; printf(“x=%d, y=%d\n”, x, y); […]

int 和int *之间的区别作为函数参数

这是一个面试问题: int []和int*之间有什么区别,它们都是函数的输入参数。 f(int a[] , int* b) 我的回答: 对于f() ,它们具有相同的function。 第一个是a[]第一个元素的起始位置。 第二个指向一个int 。 但是,如何在不传递其他论据的情况下将它们彼此区分开来?