嗨,我有以下代码
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define min(x, y)(x < y)?(x):(y)
#define SIZE 1000
int valormenor(int a[], int n)
{
if(n == 1)
return a[0];
else
return min(a[0], valormenor(a + 1, n - 1));
}
int main(void)
{
int arr[SIZE] = {0}, i;
srand (time (NULL));
for(i = 0; i < SIZE; i++)
arr[i] = rand() % SIZE;
arr[5] = -1;
printf("%d\n", valormenor(arr, SIZE));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
关键是不明白,因为找到最小数字需要太长时间,我的理论是这个递归函数实现得很糟糕,你声称谁?
在此代码中释放内存的正确方法是什么?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( void ){
char *string1, *string2;
string1 = (char*) malloc(16);
strcpy(string1, "0123456789AB");
string2 = realloc(string1, 8);
printf("string1 Valor: %p [%s]\n", string1, string1);
printf("string2 Valor: %p [%s]\n", string2, string2);
free(string1);
free(string2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于两个指针指向同一个方向
我找到了这段代码
int strlen_my(const char *s)
{
int len = 0;
for(;;)
{
unsigned x = *(unsigned*)s;
if((x & 0xFF) == 0) return len;
if((x & 0xFF00) == 0) return len + 1;
if((x & 0xFF0000) == 0) return len + 2;
if((x & 0xFF000000) == 0) return len + 3;
s += 4, len += 4;
}
}
Run Code Online (Sandbox Code Playgroud)
我很想知道它是如何工作的.¿任何人都可以解释它是如何工作的?
我遇到了以下代码
#include <stdio.h>
int main(void)
{
long long P = 1,E = 2,T = 5,A = 61,L = 251,N = 3659,R = 271173410,G = 1479296389,
x[] = { G * R * E * E * T , P * L * A * N * E * T };
puts((char*)x);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
案件是我不太明白它是如何工作的,这让我非常困惑.有人可以详细解释一下吗?
编辑:还有一件事,如何打印"Hola mundo!" (西班牙语中的"Hello world")类似地?
案例是我正在研究我在互联网上找到的引起我注意的代码,是这样的:
#include <stdio.h>
#include <stdint.h>
#define NEG ~0x0LL
void ITOC(int8_t *vec, int n)
{
int8_t *p = vec;
for(; n; n /= 10) *p++ = n % 10;
}
void ncmp(int8_t *buf, int y)
{
int tmp, i = 0;
for (; y ; y/=10)
{
tmp = y % 10;
for(i = 0; i < 8; i++)
if(buf[i] == tmp && buf[i] != -1)
{
buf[i] = -1;
break;
}
}
}
int main(void)
{
int8_t buf[8];
int y …Run Code Online (Sandbox Code Playgroud) 我找到了strcmp函数的一个实现,我把它展示给了一个朋友,他说下面的"值得注意的是它并不总是返回两个不同字符之间的差异;它实际上允许返回任何整数,前提是符号是与字节之间的差异相同." 然后没有给我进一步解释,代码就是这个
int
strcmp(s1, s2)
register const char *s1, *s2;
{
while (*s1 == *s2++)
if (*s1++ == 0)
return (0);
return (*(const unsigned char *)s1 - *(const unsigned char *)(s2 - 1));
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释什么是错误吗?什么样的字符串可以导致失败?
我有一个函数的问题,它接收一个结构数组作为参数,当尝试使用运算符访问数组元素时出现问题 - >
#include <stdio.h>
typedef struct{
int order;
}record;
void entry(record*reg, size_t num_regs);
int main(void){
record reg[10];
entry(reg, sizeof reg / sizeof reg[0]);
return 0;
}
void entry(record*reg, size_t num_regs){
size_t i;
for (i = 0; i < num_regs; i++){
reg[i]->order = i;
printf("\n order = %d", reg[i]->order);
}
}
Run Code Online (Sandbox Code Playgroud)
如果您尝试编译,则抛出此错误
*error #2140: Type error in argument 1 to 'ingreso'; expected 'registro * *' but found 'registro *'.*
Run Code Online (Sandbox Code Playgroud)
因为它会引发此错误以及如何解决它?
我有以下代码将数字转换为数字罗马
#include <stdlib.h>
#include <stdio.h>
void roman(char *s, unsigned int n)
{
if (n == 0)
{
fputs("Roman numeral zero does not exist ", stderr);
exit(EXIT_FAILURE);
}
#define digit(loop, num, c) \
loop (n >= num) \
{*(s++) = c; \
n -= num;}
#define digits(loop, num, c1, c2) \
loop (n >= num) \
{*(s++) = c1; \
*(s++) = c2; \
n -= num;}
digit ( while, 1000, 'M' )
digits ( if, 900, 'C', 'M' )
digit …Run Code Online (Sandbox Code Playgroud) 我已经看到以这种方式声明的某些函数:
char* encipher(const char *src, char *key, int is_encode);
Run Code Online (Sandbox Code Playgroud)
我不明白这一部分:
char* encipher
Run Code Online (Sandbox Code Playgroud)
数据类型后的星号是什么意思?