Tag: 参数

如何在C中创建一个虚拟shell?

我有一个带有参数的虚拟shell程序。 但是,我希望它不带参数,而是提示让用户输入可执行程序和参数的名称。 例如: $dummyshell >(executable program and parameters go here) 这是我到目前为止的代码: #include #include #include #include #include #include #define BUFFER_SIZE 1<<16 #define ARRAY_SIZE 1<= &bufCmdArgs[cmdArgsSize])) break; } for (p=n=0; bufCmdArgs[n]!=NULL; n++){ if(strlen(bufCmdArgs[n])>0) cmdArgs[p++]=bufCmdArgs[n]; } *nargs=p; cmdArgs[p]=NULL; } int main(int argc, char *argv[], char *envp[]){ char buffer[BUFFER_SIZE]; char *args[ARRAY_SIZE]; char hflag = ‘N’; int *retStatus; size_t nargs; […]

将指针转换为浮点数或使用指针参数指向函数

我试图准确理解这行代码是什么,因为我正在学习C. int (*f) (float *) 我知道第一部分是指向名为f的int的指针,但第二部分是我不确定的。 这会被归类为指向名为f的int的指针,该指针被强制转换为指向浮点数的指针 要么 这是一个名为f的指针,指向带有指针参数的函数。 如果有人可以帮助并且可能解释为什么或两者之间的差异,因为我在理解这个概念时遇到一点麻烦,那将会很棒。 谢谢!

将函数作为参数传递给其他函数

我一直在读这个主题。 我已经提到了很多可能的解决方案,所以请不要将我的问题标记为重复,只需要解决这个问题。 我有一个函数来计算一些代码的执行时间。 此代码将作为参数发送(将是一个函数)。 这是计算时间的函数: double executionTime( /* HERE I WANNA PASS THE FUNCTION TO CALCULATE EXECTIME*/ ) { LARGE_INTEGER frequency; LARGE_INTEGER start; LARGE_INTEGER end; double interval; QueryPerformanceFrequency(&frequency); QueryPerformanceCounter(&start); // HERE GOES CODE TO PROCCESS QueryPerformanceCounter(&end); interval = (double) (end.QuadPart – start.QuadPart) / frequency.QuadPart; return (interval); } 我试过这个(和另一种方式,但它是最明显的): double executionTime( void (*f)() ) { LARGE_INTEGER […]

为什么不能在函数中修改结构的成员变量?

我很好奇为什么在作为参数传递给函数时不能修改结构的变量。 我知道参数是按值传递的,但是在传递struct变量时,您将其引用作为值传递。 当然,C不会在堆栈上创建结构的副本,因为传入的结构引用的值与将结构的指针传递给函数的值相同。 #include #include typedef struct String { char* name; int x; } String; /* Func Protos */ void foo(String str); void bar(String * str); void banjo(String str); int main() { String str = *(String *)malloc(sizeof(String)); str.x = 5; /* x == 5 */ foo(str); /* x == 5 still */ printf(“%d\n”, str.x); banjo(str); […]

什么时候pthread_attr_t不是NULL?

除了pthread_attr_t之外,来自POSIX线程的pthread_create的所有参数都非常简单易懂。 pthread_attr_t是什么,如何以及何时不应该被NULL初始化? 我浏览了Linux 手册页 。 我发现有关pthread_attr_t的描述是: 句法: int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine) (void*),void *arg); 说明: The attr argument points to a pthread_attr_t structure whose contents are used at thread creation time to determine attributes for the new thread; this structure is initialized using pthread_attr_init(3) and related functions. If attr is NULL, then […]

C函数参数可以设置变量吗?

我无法弄清楚如何在下面的C代码中设置firstname和lastname变量。 printf(“Hello, %s, %s\n”, firstname, lastname); 它看起来readln函数的char [s]参数是设置firstname和lastname。 这是可能的,如果是这样的话,我可以做一些研究。 谢谢 编辑:下面是一个更简单的版本。 看起来参数是设置变量。 int foo(char s[]){ s[0]=’w’; s[1]=’\0′; return 5; } int main() { char name[2]; int wtf; wtf = foo(name); printf(“%s\n”, name); } 参数char s []是设置名称 #include #define STRLEN 5 int readln(char s[], int maxlen) { char ch; int i; int chars_remain; i = 0; […]

将&var传递给* var和var传递给var有什么区别?

基本上,我想知道为什么这个(将list的内存地址作为参数传递): void init_lista (elemPtr *list) { *list = NULL; } int main(){ elemPtr list; init_list(&list); //[…] } 与此不同(仅传递列表的内容): void init_lista (elemPtr list) { list = NULL; } int main(){ elemPtr list; init_list(list); //[…] } OBS: elemPtr是结构的指针类型( typedef struct elem *elemPtr )。 我从&和*理解的是,第一个将获得var的内存地址,后者将获得它引用的值。 通过这个概念,两个代码段应该是等价的,但第一个运行正常,而第二个编译,但给我一个运行时错误。 这是为什么?

函数参数中的int * vs int vs int(*)。 我应该使用哪一个?

在C编程语言中,有许多不同的方法来声明函数的参数,该函数将数组作为通过指针传递的参数。 我准备了一个例子,告诉你我的意思。 它是C ++中std::accumulate函数的一个实现。 它是一个函数,它可以在数组中添加所有元素并返回结果。 我可以这样写: int accumulate(int n, int *array) { int i; int sum = 0; for (i = 0; i < n; ++i) { sum += array[i]; } return sum; } 这也可以写到这(这意味着完全相同): int accumulate(int n, int array[]) { int i; int sum = 0; for (i = 0; i < n; ++i) […]

何时将指针作为参数传递给结构,何时将指针传递给指向结构的指针?

我的问题是关于以下代码。 #include #include struct node { int v; struct node * left; struct node * right; }; typedef struct node Node; struct bst { Node * root; }; typedef struct bst BST; BST * bst_insert(BST * tree, int newValue); Node * bst_insert_node(Node * node, int newValue); void bst_traverseInOrder(BST * tree); void bst_traverseInOrderNode(Node * node); int […]

为什么不需要将参数传递给qsort比较器函数?

以下代码取自此处 。 * qsort example */ #include #include int values[] = { 40, 10, 100, 90, 20, 25 }; int compare (const void * a, const void * b) { return ( *(int*)a – *(int*)b ); } int main () { int n; qsort (values, 6, sizeof(int), compare); for (n=0; n<6; n++) printf ("%d ",values[n]); […]