我一直在寻找char16_tand char32_t,因为我正在使用 Unicode,而我在网上能找到的只是它们在里面uchar.h。我在 iOS SDK(不是 macOS 的,出于某种原因)中发现了上述标题,但其中没有这样的类型。不过,我在不同的标题中看到了它们,但是我找不到它们的定义位置。此外,互联网上的信息充其量是稀缺的,所以我有点迷失在这里;但我确实读过wchar_t不应该用于Unicode,这正是我到目前为止所做的,所以请帮助:(
我想计算a b mod c where a,b并且c是整数值。有没有一种有效的方法来做到这一点?
pow(a, b) % c 似乎不起作用。
在最近的一篇文章中,我意识到在分配结构变量时,与将结构类型传递给sizeof(). 这基本上是因为前者比后者更能适应代码更改。
这表明,在下面的代码中,方法 1被认为是比方法 2更好的做法。
typedef struct X_ {
int x;
int y;
int z;
} X;
int main() {
X* obj1 = malloc(sizeof(*obj1)); // ----> method 1
X* obj2 = malloc(sizeof(X)); // ----> method 2
return 0;
}
Run Code Online (Sandbox Code Playgroud)
问题是,obj1在方法 1 中取消引用的有效性如何?Inside malloc,obj1仍然是未构造/未初始化的内存,这表明对obj1内部发生的解除引用sizeof()不应该是有效的。
让我猜猜是什么使方法 1有效。这是因为sizeof()编译器将取消引用的编译时操作obj1转换为方法 2吗?
有人可以参考相关的C标准详细说明这个的技术有效性吗?
功能strdup()并strndup()最终纳入即将推出的 C23 标准:
7.24.6.4
strdup函数概要
Run Code Online (Sandbox Code Playgroud)#include <string.h> char *strdup(const char *s);该
strdup函数在分配的空间中创建由 指向的字符串的副本s,就像通过调用 一样malloc。返回
该strdup函数返回一个指向重复字符串的第一个字符的指针。返回的指针可以传递给free. 如果无法分配空间,则该strdup函数返回空指针。7.24.6.5
strndup函数概要
Run Code Online (Sandbox Code Playgroud)#include <string.h> char *strndup(const char *s, size_t size);该
strndup函数创建一个字符串,该字符串初始化为不超过size由 指向的数组的初始字符s,直到第一个空字符(以先到者为准),在分配的空间中,就像调用 一样malloc。如果 by 指向的数组的s第一个字符中不包含 nullsize,则将 null 附加到数组的副本中。返回
该strndup函数返回一个指向所创建字符串的第一个字符的指针。返回的指针可以传递给free. 如果无法分配空间,则该strndup函数返回空指针。
strnlen为什么不考虑包含POSIX-2008 函数?
#include …Run Code Online (Sandbox Code Playgroud) 我刚刚了解了 C 中的流缓冲。据我了解,printf()C 中是缓冲的,并且仅在遇到新行、缓冲区已满或我们stdout手动刷新时才打印。然后我尝试这些代码行:
#include <stdio.h>
int main() {
printf("Hello world");
while(1);
}
Run Code Online (Sandbox Code Playgroud)
理论上,控制台不会收到任何内容,因为Hello world仍在缓冲区中。但由于某些原因,我的控制台仍然显示该字符串。为什么会这样呢?
编辑:我在 Windows 10 中使用命令提示符。
我有一个 C 程序,我在其中定义和使用我自己的printf()函数。但是,仅输出第一个printfie 。printf("Hello World 1\n");但为什么?
int printf(const char *__restrict __format, ...);
int printf(const char *__restrict __format, ...) {}
int main() {
printf("Hello World 1\n");
printf("Hello World %d\n", 2);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出是:
Hello World 1
Run Code Online (Sandbox Code Playgroud)
但我不明白为什么
#include <stdio.h>
int main(void)
{
int a = 0, b = 1;
a = (a = 5) && (b = 0);
printf("%d", a);
printf("%d", b);
}
Run Code Online (Sandbox Code Playgroud)
变量的值a被更新两次,这违反了 C 标准。所以我认为这将取决于编译器。我在很多在线 C 编译器中运行了这段代码,所有编译器都给出了相同的输出。
我试图解决这个问题:给定一个数字 N 和两个包含 N 个元素的 A、B 数组,确定 B 是否包含与 A 相同的元素(不一定位于相同位置)。
将有三个输入:
第一行包含 N 个元素
第二行包含数组 A 的 N 个元素
第三行包含数组 B 的 N 个元素
例子:
输入:
4
4 2 3 7
2 3 4 9
Run Code Online (Sandbox Code Playgroud)
输出:no
输入:
5
5 1 1 9 3
1 9 1 5 3
Run Code Online (Sandbox Code Playgroud)
输出:yes
我的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n;
scanf("%d", &n);
long int a[n], b[n];
for (int i = 0; i < n; i++)
{
scanf("%ld", &a[i]); …Run Code Online (Sandbox Code Playgroud) 我正在尝试在定义的位置添加一个角色.我已经创建了一个新函数,为一个char分配一个内存,在该位置之后保存字符,然后在定义的位置添加我的字符,现在我不知道如何在该位置之后擦除字符以连接保存的字符串.有解决方案吗
这是我的功能的开始:
void appendCharact(char *source, char carac, int position) {
source = realloc(source, strlen(source) * sizeof(char) + 1); //Get enough memory
char *temp = source.substr(position); //Save characters after my position
source[position] = carac; //Add the character
}
Run Code Online (Sandbox Code Playgroud)
编辑:我正在尝试实现另一个"野蛮"的解决方案,在调试模式下,我可以看到我差不多是我的新字符串,但看起来我无法删除旧指针...
void appendCharact(char *source, char carac, int position) {
char *temp = (char *)malloc((strlen(source) + 2) * sizeof(char));
int i;
for(i = 0; i < position; i++) {
temp[i] = source[i];
}
temp[position] = carac;
for (i = position; i < strlen(source); i++) …Run Code Online (Sandbox Code Playgroud) 错误如下(倒数第二行):
Error: no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'Complex()')|
Run Code Online (Sandbox Code Playgroud)
这是代码:
#include <iostream>
using namespace std;
class Complex {
private:
int a, b;
public:
Complex(int x, int y) {
a = x;
b = y;
}
Complex() {
a = 0;
b = 0;
}
friend ostream& operator << (ostream &dout, Complex &a);
friend istream& operator >> (istream &din, Complex &a);
};
ostream& operator << (ostream &dout, Complex &a) {
dout << a.a << " + …Run Code Online (Sandbox Code Playgroud)