- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个二维数组 A
和一个指向其中某处的指针 ptr
。我知道如何计算行号,但表达式的实际类型似乎不可移植:
#include <stdio.h>
int main(void) {
int A[100][100];
int *ptr = &A[42][24];
printf("row number is %d\n", (ptr - A[0]) / (sizeof(A[0]) / sizeof(A[0][0])));
printf("col number is %d\n", (ptr - A[0]) % (sizeof(A[0]) / sizeof(A[0][0])));
return 0;
}
clang
编译器这样提示:
warning: format specifies type 'int' but the argument has type 'unsigned long' [-Wformat]
gcc
给出了类似的警告,但在 Windows 上,我得到了不同的诊断:
warning: format specifies type 'int' but the argument has type 'unsigned long long' [-Wformat]
printf
而没有丑陋的 Actor 阵容?
最佳答案
2 个指针的差值是 ptrdiff_t
类型,这是 <stddef.h>
中定义的有符号整数类型。此类型的 printf
长度修饰符是 t
。 2个指针的差异可以直接打印:
printf("pointer difference: %td\n", ptr - A[42]);
(sizeof(A[0]) / sizeof(A[0][0]))
的大小是
size_t
,无符号类型 f 是
sizeof
中定义的
<stddef.h>
运算符的结果。
printf
长度修饰符是
z
。对象的大小可以直接打印:
printf("array size in bytes: %zu\n", sizeof(A));
ptrdiff_t
至少能够表示
-65535
和
65535
之间的值,而
size_t
必须具有至少
0
到
65535
的范围。
ptrdiff_t
除以
size_t
的类型是什么?
Otherwise (if both operands have integer types), the integer promotions are performed on both operands. Then the following rules are applied to the promoted operands:
If both operands have the same type, then no further conversion is needed. Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank is converted to the type of the operand with greater rank.
Otherwise, if the operand that has unsigned integer type has rank greater or equal to the rank of the type of the other operand, then the operand with signed integer type is converted to the type of the operand with unsigned integer type.
Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, then the operand with unsigned integer type is converted to the type of the operand with signed integer type.
Otherwise, both operands are converted to the unsigned integer type corresponding to the type of the operand with signed integer type.
ptrdiff_t
和
size_t
的实际类型,结果的类型可能不同:
int
可以表示其类型的所有值,则将其转换为
int
,如果
unsigned
可以,则转换为
unsigned
,否则类型不变。
size_t
小于
int
,
size_t
被提升为
int
,一种有符号类型,并且除法作为有符号除法执行,两个操作数首先转换为更大类型的
int
和
ptrdiff_t
(很可能也是
int
在这个案例)。
size_t
的类型为
unsigned int
且
ptrdiff_t
的类型为
int
,则
ptrdiff_t
将转换为
unsigned int
并且除法作为结果类型为
unsigned int
的无符号除法执行。
size_t
是
unsigned int
并且
ptrdiff_t
是类型
long int
,那么
size_t
操作数被转换为类型
long int
,除法是有符号除法,结果类型是
long int
。
size_t
是
unsigned long int
并且
ptrdiff_t
是
long int
类型(Linux 和 OS/X 64 位),则
ptrdiff_t
将转换为
unsigned long int
并且除法是具有结果类型
unsigned long int
的无符号除法。
size_t
为
unsigned long long int
且
ptrdiff_t
为
long long int
类型(Windows 64 位),则
ptrdiff_t
将转换为
unsigned long long int
并且除法是具有结果类型
unsigned long long int
的无符号除法。
size_t
和
ptrdiff_t
类型的其他组合,从而为结果类型带来更多可能性,例如
long long int
。
size_t
和
ptrdiff_t
。
printf("row number is %d\n", (int)((ptr - A[0]) / (sizeof(A[0]) / sizeof(A[0][0]))));
int row = (ptr - A[0]) / (sizeof(A[0]) / sizeof(A[0][0])));
printf("row number is %d\n", row);
size_t
可能大于
unsigned long long
):
printf("row number is %llu\n", 0ULL + (ptr - A[0]) / (sizeof(A[0]) / sizeof(A[0][0])));
printf
的
%zu
格式
size_t
和
%td
ptrdiff_t
,因为表达式的类型不一定是
size_t
而不是
ptrdiff_t
。对于 Windows 用户来说更糟糕的是,Microsoft C 运行时库不支持这些标准长度说明符。正如 Jean-François Fabre 所建议的,对于使用
gcc
编译 C 代码并生成 native Windows 应用程序的 MingW 用户,有一种解决方法:在命令行上指定
-D__USE_MINGW_ANSI_STDIO
告诉
gcc
使用自己的
printf
版本而不是 Microsoft 的版本运行。
ptr - A[0]
不指向数组的第一行,则表达式
ptr
实际上会调用未定义的行为,如
6.5.6 Additive operators 中所述:
- When two pointers are subtracted, both shall point to elements of the same array object, or one past the last element of the array object; the result is the difference of the subscripts of the two array elements.
关于c++ - (ptr - A[0])/(sizeof(A[0])/sizeof(A[0][0])) 的类型是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40329101/
这个问题已经有答案了: Why isn't sizeof for a struct equal to the sum of sizeof of each member? (13 个回答) 已关闭 8
先生。 Stroustrup 在他的新书(TCPL 第 4 版)第 149 页写下了以下内容 1 N && sizeof(long)<=N任何 N 值的实现,更不用说任何人都会考虑使用 wchar_t
如 [5.3.3/3] 所述(expr.sizeof,工作草案): The sizeof operator can be applied to a pointer to a function, but
从C标准来看,int至少有16bit,long至少有32bit,long long如果有的话至少有64bit(有些平台可能不支持)。只是想知道标题中的句子是否总是正确的。 最佳答案 没有。该标准仅定义
我运行的是 Windows 7(64 位)。 这个问题与此处找到的问题相同: long on a 64 bit machine 但更深入,因为它处理更多的数据类型并适用到 C 或 C++,而不是 C#
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 8年前关闭。 Improve this
这个问题在这里已经有了答案: Length of array in function argument (9 个回答) 关闭 9 年前。 #include void printS(char []);
我承认这三个都有不同的含义。但是,我不明白这些具体情况适用于哪些特定情况。任何人都可以分享每个例子吗?谢谢。 malloc(sizeof(int)) malloc(size
To avoid things quietly breaking if you change the array size, I suggest std::copy(a, a + sizeof(a)/
我在 python 中注意到以下事实: >>> (1, 2, 3).__sizeof__() 48 >>> [1, 2, 3].__sizeof__() 64 我理解列表和元组之间的区别,但我希望它们
是否存在与指针大小相同的整数类型?保证所有微架构? 最佳答案 根据 this Wikipedia page ,在 C99 中,您的 stdint.h header 可能声明了 intptr_t 和 u
我注意到 int 和 double 的大小与使用函数 MPI_Type_size(MPI_INT, &MPI_INT_SIZE); 计算的不同。这是否意味着 sizeof(MPI_INT) 返回了错误
这个问题已经有答案了: How to find the size of an array (from a pointer pointing to the first element array)? (
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
为什么 sizeof 运算符返回的结构大小大于该结构成员的总大小? 最佳答案 这是因为添加了填充以满足对齐约束。 Data structure alignment影响程序的性能和正确性: 未对齐的访问
我是一名优秀的程序员,十分优秀!