心无尘土
一段平凡的日子,一段流逝的岁月
1.5 char* p1, p2; 来声明两个指针失败的原因。作成中。。。。。。。。。。。。。。。。。。


    结果是p1为指针,p2仅仅为char.
The * in a pointer declaration is not part of the base type; it is part of the declarator containing the name being declared (see question 1.21). That is, in C, the syntax and interpretation of a declaration is not really
type identifier ;
but rather
base_type thing_that_gives_base_type ;
where ``thing_that_gives_base_type''--the declarator--is either a simple identifier, or a notation like *p or a[10] or f() indicating that the variable being declared is a pointer to, array of, or function returning that base_type. (Of course, more complicated declarators are possible as well.)

In the declaration as written in the question, no matter what the whitespace suggests, the base type is char and the first declarator is ``* p1'', and since the declarator contains a *, it declares p1 as a pointer-to-char. The declarator for p2, however, contains nothing but p2, so p2 is declared as a plain char, probably not what was intended. To declare two pointers within the same declaration, use

	char *p1, *p2;
Since the * is part of the declarator, it's best to use whitespace as shown; writing char* invites mistakes and confusion.

comp.lang.c FAQ list ·Question 1.6

Q: I'm trying to declare a pointer and allocate some space for it, but it's not working. What's wrong with this code?

char *p;
*p = malloc(10);

A: The pointer you declared is p, not *p. See question 4.2.


posted on 2006-07-28 14:31 Yama的家 阅读(310) 评论(0)  编辑 收藏 引用 所属分类: 标准C
只有注册用户登录后才能发表评论。

<2006年1月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

常用链接

留言簿(5)

随笔分类(66)

相册

积分与排名

  • 积分 - 41757
  • 排名 - 137

最新评论

阅读排行榜