免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: 无风之谷
打印 上一主题 下一主题

对C语言指针的讨论:应当如何理解、使用指针?《狂人C:程序员入门必备》有奖试读 [复制链接]

论坛徽章:
0
331 [报告]
发表于 2011-01-05 15:05 |只看该作者
弱弱地问一个与这贴无关的问题
我怎么才能一下子找到306楼?
hanxuaiztt 发表于 2011-01-05 14:43



    不知道。
    念“芝麻开门”?俺没试过

论坛徽章:
0
332 [报告]
发表于 2011-01-05 15:08 |只看该作者
本帖最后由 KBTiller 于 2011-01-05 15:15 编辑
额,更若若的问一下,偶只是请楼主提出有力的论据来证明你的观点。
magicrobot1984 发表于 2011-01-05 15:03


您的意思是说,您根本找不到反驳我观点的论据,是吗?!

不过好像您根本就没“问”什么

论坛徽章:
0
333 [报告]
发表于 2011-01-05 15:25 |只看该作者
额,别生气,偶只是请楼主提出有力的论据来证明你的观点。
楼主既然能写书,那肯定是深厚的功力和C的经验,那么相信我的问题对于楼主来讲是个小case, 而且既然能写出来肯定也是有足够的客观依据,我这菜鸟一只,楼主不介意提供能够支撑您论点的依据吧

论坛徽章:
0
334 [报告]
发表于 2011-01-05 15:28 |只看该作者
如果您想说我的问题是什么,那我可以重复:
1. &运算付是取地址还是取指针,依据
2. unsigned char与signed char,您说是编译器决定类型,依据

我是菜鸟,而您的书也是给入门的人看的,看不明白肯定要问咯,相信您不介意吧。

论坛徽章:
0
335 [报告]
发表于 2011-01-05 20:45 |只看该作者
如果您想说我的问题是什么,那我可以重复:
1. &运算付是取地址还是取指针,依据
2. unsigned char与signed char,您说是编译器决定类型,依据

magicrobot1984 发表于 2011-01-05 15:28


1.&运算是求指针。理由:代码中的任何数据都有数据类型,&运算得到的是一个指针类型的数据。地址这种数据类型在C语言中根本不存在。
2.char 类型是unsigned 还是 signed 由编译器决定。依据:C标准。

论坛徽章:
0
336 [报告]
发表于 2011-01-05 22:32 |只看该作者
前面的讨论有点眩晕!对于C,我在写代码的时候,通常会思考我的C代码如何在内存中运作的,指针也好,地址也好,只是一种表达方式而已,仁者见仁,智者见智,看自己站在什么角度!
就我自己来说,地址我会认为是内存的一种索引标号,跟HW相关(学过计算机原理的都知道地址线,数据线这些东东),指针不过是内存中具体数据的一种类型(这种类型表示其内容记录的是一个内存的一个索引标号)!
对C语言来说,脱离HW单纯的讲语言个人认为是对C的不负责任!

以上!

论坛徽章:
0
337 [报告]
发表于 2011-01-05 22:55 |只看该作者
本帖最后由 magicrobot1984 于 2011-01-05 22:56 编辑

额,学习了C99标准,相信楼主即然用了C99做依据,那么我们的讨论应该都是建立在C99的基础之上的,现在也只拿C99说事

额,看到段关于void *的说明,不知道能不能让楼主对于void *是否为虚无这个观点有些别的想法,先看到这个,先贴:
26 A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.39) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same

脚注:39) The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.

论坛徽章:
0
338 [报告]
发表于 2011-01-05 22:56 |只看该作者
前面的讨论有点眩晕!对于C,我在写代码的时候,通常会思考我的C代码如何在内存中运作的,指针也好,地址也 ...
hoverlin 发表于 2011-01-05 22:32



    握爪,赞同

论坛徽章:
0
339 [报告]
发表于 2011-01-05 23:21 |只看该作者
4 There are five standard signed integer types, designated as signed char, short int, int, long int, and long long int. (These and other types may be designated in several additional ways, as described in 6.7.2.) There may also be implementation-defined extended signed integer types.2 The standard and extended signed integer types are collectively called signed integer types.29)

6 For each of the signed integer types, there is a corresponding (but different) unsigned integer type (designated with the keyword unsigned) that uses the same amount of storage (including sign information) and has the same alignment requirements. The type _Bool and the unsigned integer types that correspond to the standard signed integer types are the standard unsigned integer types. The unsigned integer types that correspond to the extended signed integer types are the extended unsigned integer types. The standard and extended unsigned integer types are collectively called unsigned integer types.30)

脚注:30) Therefore, any statement in this Standard about unsigned integer types also applies to the extended unsigned integer types

15 The three types char, signed char, and unsigned char are collectively called the character types. The implementation shall define char to have the same range, representation, and behavior as either signed char or unsigned char.35)

脚注:35) CHAR_MIN, defined in <limits.h>, will have one of the values 0 or SCHAR_MIN, and this can be used to distinguish the two options. Irrespective of the choice made, char is a separate type from the other two and is not compatible with either.

我英文不好,所以看这几个条件的时候,将其理解为unsigned char也是C99中明文规定的,不知道这样的理解是否正确。另外,C99中规定了unsigned char与signed char的范围:
— minimum value for an object of type signed char
SCHAR_MIN -127 // -(27 - 1)
— maximum value for an object of type signed char
SCHAR_MAX +127 // 27 - 1
— maximum value for an object of type unsigned char
UCHAR_MAX 255 // 28 - 1

所以,没有明白为什么说C99没有定义unsigned char这个是问题之一,问题之二,C99中又是何处说了需要编译器自己选择char是unsigned还是signed,对于两个表示范围差剧如此之大的类型,选择的依据是什么?

对于嵌入式平台,unsigned确实优于signed,因此编译选项可以配置char默认为unsigned,但这个并不足以说明是编译器能够自己去选择。

论坛徽章:
0
340 [报告]
发表于 2011-01-05 23:51 |只看该作者
本帖最后由 magicrobot1984 于 2011-01-05 23:55 编辑

9 Anaddress constant is a null pointer, a pointer to an lvalue designating an object of static storage duration, or a pointer to a function designator; it shall be created explicitly using the unary & operator or an integer constant cast to pointer type, or implicitly by the use of an expression of array or function type. The array-subscript [] and member-access .
and -> operators, the address & and indirection * unary operators, and pointer casts may be used in the creation of an address constant, but the value of an object shall not be accessed by use of these operators.

还是&运算符,我今天算是没事给自己找事,看了C99.。。扯转来,&的结果就是个地址常量,要说C没有地址这种类型呢确实比较那个啥,偶不多说了,看C99说的。

— The value of an object is accessed by an array-subscript [], member-access . or −>, address &, or indirection * operator or a pointer cast in creating an  ddress constant(6.6).
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP