我找到了一个递归函数,让我有点惊讶这个函数计算出现在数组中的所有负数:
int count_negative(int arr[], int n)
{
if ( n > 0 )
return (*arr < 0) + count_negative( ++arr, n - 1 );
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释这一行:
return (*arr < 0) + count_negative( ++arr, n-1 );
Run Code Online (Sandbox Code Playgroud)
谢谢
这样做是不明确的行为?
char *a = "hello";
char b[] = "abcd";
a = b;
Run Code Online (Sandbox Code Playgroud)
我的编译器没有抛出任何警告,具有最大警告级别.
我制作了这段代码来获得fibnacci系列的术语总和:
int main() {
int previous, current = 0, next = 1,
sum = current, threshold;
printf("Enter the threshold: ") ;
scanf("%d", &threshold) ;
printf("Fibonacci series: %d", current) ;
while (sum < threshold)
{
previous = current;
current = next;
next = previous + current;
printf(" + %d", current) ;
sum += current;
if((log10(sum) + 1) >= 7)
break;
}
printf(" = %d\n", sum);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我需要你从系列的那一端告诉我所有术语的总和大于7位数.我试过这个,我不知道它是否方便:
if((log10(sum) + 1) >= 7)
break;
Run Code Online (Sandbox Code Playgroud)
因为我已经了解log10()消耗了大量的处理器时间和资源,¿有更有效的方法吗?
我试图找到一系列数字的平均值:
double moving_average((unsigned num )
{
double temp;
temp = calculate_number(num);
if ( num > 0 ) moving_average( num - 1 );
else
return 0;
return temp * (n - 1) / num;
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试实现指示此页面:
http://en.wikipedia.org/wiki/Moving_average
但我这样做的代码并没有给我正确的平均值 - 实现的问题是什么?
我有以下代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct persona
{
char *nombre;
int edad;
int sexo;
} Persona;
typedef struct
{
int size;
Persona vecPersona[];
} Array;
Array* getArrayInstance()
{
Array *vec;
vec = (Array*) malloc (sizeof(Persona));
vec->size = 0;
return vec;
}
void push_back(Array ** vec, Persona tipito)
{
(*vec)->vecPersona[(*vec)->size] = tipito;
(*vec)->size++;
printf("%d-", (*vec)->size);
int newSize = (*vec)->size*2-(*vec)->size+1;
Array *tmp = realloc((*vec), newSize*sizeof(Persona));
if(tmp)
*vec = tmp;
else
(*vec)->size--;
}
void mostrarPersonas(Array *vec)
{
int i;
printf("\n\n"); …Run Code Online (Sandbox Code Playgroud) 以下代码按我的预期编译和运行.
#include <stdio.h>
int main(void) {
const char C1 = '1';
char const C2 = '3';
printf("%c %c", C1, C2);
char *pC1 = &C1; *pC1 = 'H';
char *pC2 = &C2; *pC2 = 'o';
printf("%c %c", C1, C2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
此代码调用未定义的行为?
我写了这个小函数:
int mayor(int n1, int n2, int n3, int n4, int n5) {
int mayor = n1;
for(int *p=&n2; p<=&n5; ++p)
mayor = *p;
return mayor;
}
Run Code Online (Sandbox Code Playgroud)
能够保证所有包含该内存块n1到n5是连续的?既然我得到了预期的回报值,我希望如此,但我想知道这是否安全.
c parameter-passing calling-convention memory-layout contiguous
如果我执行以下操作会发生什么
scanf("%d,%d", &i, &j);
Run Code Online (Sandbox Code Playgroud)
并提供导致匹配失败的输入?它会存储垃圾j吗?