我只是想知道以下是否是将 int 转换为 char 的正确方法
#include <stdio.h>
int main()
{
int x = 500;
printf("%hhd\n", x);
}
Run Code Online (Sandbox Code Playgroud)
另外,从上面我想知道是否应该执行以下操作来显示字符的值。
#include <stdio.h>
int main()
{
char c = 'a';
printf("%hhd\n", c);
}
Run Code Online (Sandbox Code Playgroud)
还是会printf("%d\n", c);没事的?所以,基本上我试图通过 printf 输出整数的第一个字节而不进行任何转换。
C99 标准是否定义了以下行为:
puts(s);// s != NULL but *s==""
我在 Linux 手册页中查找过这一点,但没有发现任何有用的东西。有人可以澄清一下吗?
我有一个整数数组:
int* counters = (int *) calloc(N, sizeof(int));
Run Code Online (Sandbox Code Playgroud)
必须使用基于一个的索引进行索引,例如第一个元素的索引为 1,第二个元素的索引为 2,等等。由于性能非常重要,我决定使用一个技巧:
int* oneIndexedCounters = counters - 1;
Run Code Online (Sandbox Code Playgroud)
这允许我使用基于 1 的索引,而无需从索引中减去 1:
// A[i] - contains one based indexes
for (int i = 0; i < K; i++) {
oneIndexedCounters[A[i]] += B[i]; // many times in code
// some other operations on oneIndexedCounters
}
Run Code Online (Sandbox Code Playgroud)
插入:
for (int i = 0; i < K; i++) {
counters[A[i]-1] += B[i];
// ...
}
Run Code Online (Sandbox Code Playgroud)
counters数组是由我的函数返回的,因此我无法在数组开头分配虚拟元素。
当您不取消引用该指针时,指向数组之前的一个元素是否有效(例如,当数组位于内存页边界上时)?或者是否有其他解决方案不那么棘手并且具有良好的性能?
在函数内部重新分配函数参数是不好还是好的做法,或者可能是未定义的行为?
让我用一个例子解释一下我想要做什么,这里是函数:
void
gkUpdateTransforms(GkNode *node /* other params */) {
GkNode *nodei;
if (!(nodei = node->chld))
return;
do {
/* do job */
nodei = nodei->next;
} while (nodei);
}
Run Code Online (Sandbox Code Playgroud)
选择:
void
gkUpdateTransforms2(GkNode *node /* other params */) {
/* node parameter is only used here to get chld, not anywhere else */
if (!(node = node->chld))
return;
do {
/* do job */
node = node->next;
} while (node);
}
Run Code Online (Sandbox Code Playgroud)
我检查了程序集输出,看起来是一样的,我们不需要在第二个输出中声明变量。您可能会问,如果参数类型发生变化,但第一个条件相同怎么办,因为它也需要更新。
编辑:参数是按值传递的,我的目的不是编辑指针本身
EDIT2:递归函数怎么样?如果 gkUpdateTransforms2 是递归的,会发生什么?我很困惑,因为函数会调用自身,但我认为在每次调用中,参数都会有不同的堆栈
该程序的目标是将大量整数存储在一个数组中,如下所示。它使用“池”函数来收集索引为 2 的整数,并将“池”返回给主函数。我尝试在 Xcode 11 中编译代码,但是在 main 中调用“pool”函数期间出现了这个错误“函数‘池’的隐式声明在 C99 中无效”。我该如何解决?如何将 Xcode 上的编译器更改为 C11 标准?
#include <stdio.h>
#include <stdlib.h>
#define SIZE 1000000000
int group[SIZE];
int main()
{
pool();
return 0;
}
int pool()
{
for (int index = 2; index < SIZE; index++)
{
group[index] = 1;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 积分提升和平衡有什么区别。我们可以通过说在执行任何操作之前将任何类型转换为至少 int 或 unsigned int 类型(逻辑运算符 &&、||、!比整数?
我发现C99添加_Complex了支持复杂算术.但是,我想知道为什么C99为这样一个与字段相关的功能添加新关键字(仅对科学计算有用).通过标准库支持复杂类型不是更好吗?
我刚刚i++向++i朋友解释了细节。我告诉他如何在没有优化的情况下,循环本质i++上意味着制作一个不用于任何用途的for副本。i因为i++可以用这个伪代码来描述:
tmp = i;
i = i + 1;
return tmp;
Run Code Online (Sandbox Code Playgroud)
好吧,我注意到我真的不知道一件事:我们tmp分配的内存在哪里?它会增加整个过程/功能所需的内存大小吗?(也就是说,它在堆栈上吗?)
我想是的,但是如何测试呢?当且仅当重要时我们才讨论 C99 标准和 GCC 编译器。但我更喜欢更广泛的答案,以获得对此事的一些看法。