Tag: 函数

知道数组大小的“sizeof”在C中的函数中不起作用

int main() { int laiArreglo[] = {5,8,2,3,1,4,6,9,2,10}, liElemento; printf(“\nInsert the number: “); scanf(“%d”, &liElemento); ShowNumber(laiArreglo); return 0; } void ShowNumber(int laiArreglo[]) { int liContador; printf(“\nNumbers: “); for (liContador = 0; liContador < sizeof (laiArreglo) / sizeof (int); liContador++) { printf("%d ", laiArreglo[liContador]); } } 我在main中使用(sizeof(laiArreglo)/ sizeof(int))并且它工作得很好但是,现在在function内部它不起作用,为什么? 。

如何在C中实现“私有/受限”function?

在C采访中我被问到一个非常有趣的问题:如何以一种只能从特定的g()函数调用它的方式实现函数f()。 如果g()以外的函数试图调用f(),则会导致编译器错误。 起初,我虽然可以使用函数指针完成,但我可以在运行时接近阻塞调用。 但我无法想到编译时策略。 我甚至不知道使用ansi C是否可行。 有谁有想法吗?

结构混乱

我想创建一个函数,它获取一本书的标题和作者,如果它可用或不可用则返回0或1,通过将它们与给定的结构数组进行比较…..编译器显示: structs.c:10:28: error: expected ‘)’ before ‘title’ structs.c: In function ‘main’: structs.c:59:21: error: expected expression before ‘bookRecord’ structs.c:60:13: error: expected expression before ‘bookRecord’ structs.c:61:9: warning: implicit declaration of function ‘requestBook’ structs.c:61:23: error: expected expression before ‘bookRecord’ 这是代码: #include #include #include #define TRUE 1 #define FALSE 0 #define NUM_BOOKS 5 int requestBook(bookRecord title[],bookRecord author[]){ /* compiler […]

无法使用变量参数实现函数

我试图用变量参数实现函数,但是将垃圾值作为输出。我在尝试自己实现之前已经参考过这篇文章 。可以任何人帮我解决这段代码,因为我无法理解这段代码中的错误。 /* va_arg example */ #include /* printf */ int FindMax (int n, …) { int i,val,largest,*p; p=&n; p+=sizeof(int); largest=*p; for (i=1;ival)?largest:val; } return largest; } int main () { int m; m= FindMax (7,702,422,631,834,892,104,772); printf (“The largest value is: %d\n”,m); return 0; }

如何解决“参数有不完整类型”的错误?

我是新手,我需要帮助调试我的代码。 当我编译它”forms参数1的类型不完整’和forms参数2的类型是不完整的’错误出现在 printf(“Age is %d years.\n”, calc_age(birth, current)); 而’参数1(’出生’)具有不完整类型’和’参数2(’当前’)具有不完整类型’错误出现在 int calc_age (struct date birth, struct date current) { 感谢帮助,谢谢! #include int calc_age (struct date birth, struct date current); int main(void) { struct date { int month[1]; int day[1]; int year[1]; }; struct date birth, current; char c; printf(“Input the birthdate (MM/DD/YY): “); scanf(“%d %c %d […]

使用函数更改指针包含的地址

如果我已经将指针p声明为int *p ; 在主模块中,我可以通过指定p=&a;来改变p所包含的地址p=&a; 其中a是已经声明的另一个整数变量。 我现在想用一个函数改变地址:: void change_adrs(int*q) { int *newad; q=newad; } 如果我从主模块调用此函数 int main() { int *p; int a=0; p=&a; // this changes the address contained by pointer p printf(“\n The address is %u “,p); change_adrs(p); printf(“\n the address is %u “,p); // but this doesn’t change the address return 0; } 地址内容不变。 […]

‘main’函数的返回值在哪里?

在C中,函数总是将其值返回给调用函数,而不是自身( 如果返回类型不是void )。 喜欢, int main() 但由于操作系统调用了 ‘ main ‘函数,因此它不是函数 。 那么,’main’函数是谁返回它的值? 当使用表达式返回值时,值的位置 return(0); 在节目结束?

为什么函数不会改变变量?

在这段代码中我似乎得到零,我不太熟悉为什么我不能用我创建的函数改变变量长度。 任何帮助都可能有用。 #include double get_length(double a); int main(int argc, char* argv[]) { double length = 0; get_length(length); printf(“%lf”, length); return 0; } double get_length(double a) { printf(“What is the rectangle’s length?\n”); scanf(“%lf”, &a); return a; } 打印时,返回0.0000

C字符串返回函数返回垃圾

我无法从函数返回一个字符串。 它在main方法中打印出一个垃圾值。 我在这个论坛上看到了类似的问题,但该页面上的结果对我没有帮助。 我不想将另一个变量传递给函数。 我希望能够按原样返回字符串值。 我怎么能这样做? char *LookupPath(char **argv, char **dir) { /* String Name To Be Returned */ char path_name[MAX_PATH_LEN] = {0}; char *result = malloc(sizeof(path_name)); int i; /* Check To See If File Name Is Already An Absolute Path Name */ if(*argv[0] == ‘/’) { } /* Look In Path Directories */ for(i […]

从C中的函数返回数组

由于某种原因,我的函数只返回我的数组中的第一个元素,我无法弄清楚为什么数组的其余部分超出范围。 该函数接受两个整数数组,添加它们各自的元素,并将总和放入返回的第三个数组中。 这是我的代码: #include /* count digits, white space, others */ int *sumarrays(int arr1[], size_t arr1len, int arr2[], size_t arr2len); void main() { int arr1[10]; size_t arr1len = 10; int arr2[10] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }; size_t arr2len = 10; int i; int *x; arr1[0] = 2; […]