正如标题所述,我想知道具有灵活数组成员的C结构数组如何表现.这是一个例子:
struct vector {
size_t length;
double array[];
};
Run Code Online (Sandbox Code Playgroud)
维基百科的文章说:
sizeof这种结构上的操作符需要给出柔性阵列成员的偏移量.
在我的机器上,这对应于8个字节(sizeof(size_t)).但是,当我执行以下操作时会发生什么:
显然,数组不能保存向量数据v0,因为它只有3*8字节= 24字节宽.我该如何处理这样的情况?
#define LENGTH 10
int main() {
struct vector arr[3];
struct vector *v0 = calloc(1, sizeof(*v0) + LENGTH * sizeof(v0->array[0]));
v0->length = LENGTH;
size_t i;
for (i = 0; i < v0->length; i++) {
v0->array[i] = (double) i;
}
struct vector v1;
struct vector v2;
arr[0] = *v0;
arr[1] = v1;
arr[2] = v2;
for (i = 0; …Run Code Online (Sandbox Code Playgroud) 如果我想存储一个常量字符串,
const char array[] = "Some string literal.";
Run Code Online (Sandbox Code Playgroud)
C引子加书说
然后,引用的字符串存储在作为可执行文件一部分的数据段中.只有在程序开始运行后才会分配数组的内存.那时,引用的字符串被复制到数组中.
这是否意味着内存为字符串文字分配了两次?
另一方面,当使用指针声明时,它仅为指针变量留出存储空间并将字符串文字的地址存储到其中.
const char *pt = "Some string literal.";
Run Code Online (Sandbox Code Playgroud)
这意味着只有一个字符串文字的副本,并且使用字符串文字声明指针比数组的内存效率更高?
这是一段似乎被接受而没有错误的代码:
#include <stdio.h>
#include <string.h>
int main() {
if (strcmp(1, 2))
printf(3);
}
Run Code Online (Sandbox Code Playgroud)
编译时clang -std=c11 -Weverything会产生4个警告:
badstrcmp.c:5:16: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Wint-conversion]
if (strcmp(1, 2))
^
/usr/include/string.h:77:25: note: passing argument to parameter '__s1' here
int strcmp(const char *__s1, const char *__s2);
^
badstrcmp.c:5:19: warning: incompatible integer to pointer conversion passing 'int' to parameter of type 'const char *' [-Wint-conversion]
if (strcmp(1, 2))
^
/usr/include/string.h:77:43: note: passing argument …Run Code Online (Sandbox Code Playgroud) 鉴于这是合法的
uint8_t bytes[4] = { 1, 2, 3, 4 };
Run Code Online (Sandbox Code Playgroud)
这不是:
uint8_t bytes2[4];
bytes2 = { 1, 2, 3, 4 };
Run Code Online (Sandbox Code Playgroud)
什么{ 1, 2, 3, 4 }代表?
假设它既不是右值也不是左值.一个预处理器代码糖果扩展到什么?
我最近在我的一个课程中接受了测验.问题如下:
cmp在C中编写一个函数(调用),它接受两个整数(x和y)并返回:-1ifx<y,0ifx=y,1ifx>y.写得cmp尽可能简洁.
我能想到的最简洁的功能是:
int cmp(int x, int y) {
return ((x < y) ? (-1) : ((x == y) ? (0) : (1)));
}
Run Code Online (Sandbox Code Playgroud)
但我有一种感觉,我可以用一些操作来更简洁地做到这一点.也许是&和^?的结合?这已经困扰了我过去的几天,我想知道是否有其实IS更好的方式来做到这一点?
这是为家庭作业
我必须编写一个程序,要求用户输入一个字符串,然后我的程序将输入的字符串中的偶数和奇数值分开.这是我的计划.
#include <stdio.h>
#include <string.h>
int main(void) {
char *str[41];
char odd[21];
char even[21];
int i = 0;
int j = 0;
int k = 0;
printf("Enter a string (40 characters maximum): ");
scanf("%s", &str);
while (&str[i] < 41) {
if (i % 2 == 0) {
odd[j++] = *str[i];
} else {
even[k++] = *str[i];
}
i++;
}
printf("The even string is:%s\n ", even);
printf("The odd string is:%s\n ", odd);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我尝试编译我的程序时,我得到两个警告:
因为我scanf得到了"format …
#include <stdio.h>
int main(void) {
int a[4][2] = { {11, 12}, {21, 22}, {31, 32}, {41, 42} };
int *p = a[0];
printf("%d\n", *p);
printf("%d\n", p);
printf("%d\n", a[0]);
printf("%d\n", &p);
printf("%d\n", &a[0]);
printf("%d\n", a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
看看上面的代码.
在我的理解中,因为p等于a[0],&p并且&a[0]应该具有相同的值.
但实际输出如下:
11
1772204208
1772204208
1772204200
1772204208
1772204208
Run Code Online (Sandbox Code Playgroud)
为什么&p和&a[0]不同?
什么是&p代表?
我是C的完全初学者,我遇到了我认为是一个简单的错误.我在网上查找了类似的问题,但我发现我的代码没有问题.很少,我不知道问题是什么.
这是错误:
C: Array initialization requires a brace-enclosed initializer list
这是我的完整代码
#include <stdio.h>
int main() {
char walk[10][10] = { 0 };
for (int row = 0; row < 10; row++) {
for (int col = 0; col < 10; col++) {
walk[row][col] = '.';
printf("%c", walk[row][col]);
}
}
getchar();
return 0;
}
Run Code Online (Sandbox Code Playgroud) 在标准 C 中,您如何可靠地检查写入标准 I/O 流的所有输出是否已成功保存到磁盘?
C 标准规定fclose成功时将返回 0,如果“检测到任何错误” ,则返回 EOF。
但这是否意味着“在fclose通话过程中检测到任何错误”?或者这是否意味着“自上次调用clearerr”以来检测到任何错误?
换句话说,程序只检查 的返回值就足够了fclose,还是还需要检查ferror?是否有任何实现,如果ferror返回非零,则后续调用fclose可能返回 0?
这同样适用于fflush:如果fflush返回 0,后续调用也ferror将返回 0,如果fflush返回 EOF,后续调用ferror将返回非零值是否总是这样?是否有任何实现不是这种情况?
(当然,我不考虑停电、小鬼等。是的,需要保证耐用性的程序应该使用fsync,但这超出了标准 C 的范围。)
我已经使用 C 指针有一段时间了,一切都按预期工作。然而现在我遇到了 ISO 标准,该标准规定“如果生成的指针未针对指向的类型正确对齐,则行为未定义”。
这让我不禁要问自己:
这是什么意思?
我们如何安全地将指针转换为其他类型?
我发现这个 C 代码示例据说是不正确的,因为它与更严格对齐的指针类型有关:
int main() {
char c = 'x';
int *ip = (int *)&c;
char *cp = (char *)ip;
return 0;
}
Run Code Online (Sandbox Code Playgroud)