user-0 提问: 指针类型转换

回复:
user: user-1
content: 引用 7 楼 q3733353520 的回复:[quote=引用 4 楼 zhao4zhong1 的回复:] 作为一个C程序员，对  scanf,sscanf,fscanf  printf,sprintf,fprintf 这类函数的用法，还是要做到“拳不离手，曲不离口”的。  printf里面的%和变量的一一对应关系 scanf里面的%和变量以及变量前加不加&的一一对应关系 是C代码中非常容易出错的地方，而且通常编译还不出错。 所以在编译源代码之前值得专门仔细检查一遍甚至多遍。  #include <stdio.h>  int main(void){     int n=2147483647;     int*  p=&n;     short* s=(short*)p;      printf("%p,%x,%d\n",p,*p,*p);      printf("%p,%hx,%hd\n\n",s,(unsigned short)*s,( short)*s);     printf("%p,%hx,%hd\n\n",s,( short)*s,( short)*s);      printf("%p,%hx,%hd\n\n",s,*s,*s);         return 0 ; } Size and Distance Specification The optional prefixes to type, h, l, and L, specify the “size” of argument (long or short, single-byte character or wide character, depending upon the type specifier that they modify). These type-specifier prefixes are used with type characters in printf functions or wprintf functions to specify interpretation of arguments, as shown in the following table. These prefixes are Microsoft extensions and are not ANSI-compatible.  Table R.6   Size Prefixes for printf and wprintf Format-Type Specifiers  To Specify Use Prefix With Type Specifier  long int l d, i, o, x, or X  long unsigned int l u  short int h d, i, o, x, or X  short unsigned int h u  __int64 I64 d, i, o, u, x, or X  Single-byte character with printf functions  h c or C  Single-byte character with wprintf functions h c or C  Wide character with printf functions l c or C  Wide character with wprintf functions l c or C  Single-byte – character string with printf functions h s or S  Single-byte – character string with wprintf functions h s or S  Wide-character string with printf functions l s or S  Wide-character string with wprintf functions l s or S    Thus to print single-byte or wide-characters with printf functions and wprintf functions, use format specifiers as follows.  To Print Character As Use Function With Format Specifier  single byte printf c, hc, or hC  single byte wprintf C, hc, or hC  wide wprintf c, lc, or lC  wide printf C, lc, or lC    To print strings with printf functions and wprintf functions, use the prefixes h and l analogously with format type-specifiers s and S.如何输出 7fff?[/quote]  我知道了    #include <stdio.h>  int main(void){ 	int n=2147483647; 	int*  p=&n; 	short* s=(short*)p;  	printf("%p,%x,%d\n",p,*p,*p);  	printf("%p,%hx,%hd\n\n",s,(unsigned short)*s,( short)*s); 	printf("%p,%hx,%hd\n\n",s,( short)*s,( short)*s);  	printf("%p,%hx,%hd\n\n",s,*(++s),*s);   	return 0 ; }

user: user-1
content: 引用 4 楼 zhao4zhong1 的回复:作为一个C程序员，对  scanf,sscanf,fscanf  printf,sprintf,fprintf 这类函数的用法，还是要做到“拳不离手，曲不离口”的。  printf里面的%和变量的一一对应关系 scanf里面的%和变量以及变量前加不加&的一一对应关系 是C代码中非常容易出错的地方，而且通常编译还不出错。 所以在编译源代码之前值得专门仔细检查一遍甚至多遍。  #include <stdio.h>  int main(void){     int n=2147483647;     int*  p=&n;     short* s=(short*)p;      printf("%p,%x,%d\n",p,*p,*p);      printf("%p,%hx,%hd\n\n",s,(unsigned short)*s,( short)*s);     printf("%p,%hx,%hd\n\n",s,( short)*s,( short)*s);      printf("%p,%hx,%hd\n\n",s,*s,*s);         return 0 ; } Size and Distance Specification The optional prefixes to type, h, l, and L, specify the “size” of argument (long or short, single-byte character or wide character, depending upon the type specifier that they modify). These type-specifier prefixes are used with type characters in printf functions or wprintf functions to specify interpretation of arguments, as shown in the following table. These prefixes are Microsoft extensions and are not ANSI-compatible.  Table R.6   Size Prefixes for printf and wprintf Format-Type Specifiers  To Specify Use Prefix With Type Specifier  long int l d, i, o, x, or X  long unsigned int l u  short int h d, i, o, x, or X  short unsigned int h u  __int64 I64 d, i, o, u, x, or X  Single-byte character with printf functions  h c or C  Single-byte character with wprintf functions h c or C  Wide character with printf functions l c or C  Wide character with wprintf functions l c or C  Single-byte – character string with printf functions h s or S  Single-byte – character string with wprintf functions h s or S  Wide-character string with printf functions l s or S  Wide-character string with wprintf functions l s or S    Thus to print single-byte or wide-characters with printf functions and wprintf functions, use format specifiers as follows.  To Print Character As Use Function With Format Specifier  single byte printf c, hc, or hC  single byte wprintf C, hc, or hC  wide wprintf c, lc, or lC  wide printf C, lc, or lC    To print strings with printf functions and wprintf functions, use the prefixes h and l analogously with format type-specifiers s and S.如何输出 7fff?

user: user-1
content: 引用 2 楼 user-6 的回复:注意大小端的问题啊 如何输出 7fff

user: user-2
content: 什么大端小端，什么sprintf之类的，你连值的对象表示都搞不清楚，还谈什么输出。

user: user-3
content: 引用 10 楼 u012683733 的回复:引用 9 楼 BuleRiver 的回复:有大小端问题吧short类型应该是2字节，为什么（short）*s的输出会是字节的ffff ffff？？？ 参考 C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\output.c

user: user-4
content: 尝试把一段不是声明类型的指针指向的地址强制转换未另外一种指针时，尝试用一种不属于他自身的含义去解释那段内存时，后果都是很严重的。。。

user: user-0
content: 引用 9 楼 BuleRiver 的回复:有大小端问题吧short类型应该是2字节，为什么（short）*s的输出会是字节的ffff ffff？？？

user: user-5
content: 有大小端问题吧

user: user-2
content: short* s=(short*)p;  --可能是未定义的行为。--A pointer to an object type may be converted to a pointer to a different object type. If the resulting pointer is not correctly aligned68) for the referenced type, the behavior is undefined.        别的就不评论了。以某种类型访问另一种不同的类型，还涉及到对象表示（object representation）。标准对对象表示中的字节顺序没有强加约束，输出不同自然也不奇怪。

user: user-3
content: 作为一个C程序员，对  scanf,sscanf,fscanf  printf,sprintf,fprintf 这类函数的用法，还是要做到“拳不离手，曲不离口”的。  printf里面的%和变量的一一对应关系 scanf里面的%和变量以及变量前加不加&的一一对应关系 是C代码中非常容易出错的地方，而且通常编译还不出错。 所以在编译源代码之前值得专门仔细检查一遍甚至多遍。  #include <stdio.h>  int main(void){     int n=2147483647;     int*  p=&n;     short* s=(short*)p;      printf("%p,%x,%d\n",p,*p,*p);      printf("%p,%hx,%hd\n\n",s,(unsigned short)*s,( short)*s);     printf("%p,%hx,%hd\n\n",s,( short)*s,( short)*s);      printf("%p,%hx,%hd\n\n",s,*s,*s);         return 0 ; } Size and Distance Specification The optional prefixes to type, h, l, and L, specify the “size” of argument (long or short, single-byte character or wide character, depending upon the type specifier that they modify). These type-specifier prefixes are used with type characters in printf functions or wprintf functions to specify interpretation of arguments, as shown in the following table. These prefixes are Microsoft extensions and are not ANSI-compatible.  Table R.6   Size Prefixes for printf and wprintf Format-Type Specifiers  To Specify Use Prefix With Type Specifier  long int l d, i, o, x, or X  long unsigned int l u  short int h d, i, o, x, or X  short unsigned int h u  __int64 I64 d, i, o, u, x, or X  Single-byte character with printf functions  h c or C  Single-byte character with wprintf functions h c or C  Wide character with printf functions l c or C  Wide character with wprintf functions l c or C  Single-byte – character string with printf functions h s or S  Single-byte – character string with wprintf functions h s or S  Wide-character string with printf functions l s or S  Wide-character string with wprintf functions l s or S    Thus to print single-byte or wide-characters with printf functions and wprintf functions, use format specifiers as follows.  To Print Character As Use Function With Format Specifier  single byte printf c, hc, or hC  single byte wprintf C, hc, or hC  wide wprintf c, lc, or lC  wide printf C, lc, or lC    To print strings with printf functions and wprintf functions, use the prefixes h and l analogously with format type-specifiers s and S.

user: user-0
content: 引用 2 楼 user-6 的回复:注意大小端的问题啊大神，能不能详细解释下？？请多多指教！！

user: user-6
content: 注意大小端的问题啊

user: user-0