免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 3278 | 回复: 11
打印 上一主题 下一主题

义一个数组指针, 将数组的第一个元素赋值为1,为什么编译出错,我是一名初学者. [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-07-10 16:13 |只看该作者 |倒序浏览
#include <stdio.h>
int main(int argc, char *argv[]){
        while(1){
                int (*p)[2];
                *p = 1;
                return 1;
        }
}

定义一个数组指针, 将数组的第一个元素赋值为1,为什么编译出错,我是一名初学者.

错误:赋值时类型不兼容

论坛徽章:
0
2 [报告]
发表于 2006-07-10 16:28 |只看该作者

回复 1楼 yangbo8481 的帖子

怎么没人回啊,一个指点....

论坛徽章:
0
3 [报告]
发表于 2006-07-10 16:30 |只看该作者
>>*p = 1;
(*p)[0] = 1;

[ 本帖最后由 FreeGnu 于 2006-7-10 16:32 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2006-07-10 16:42 |只看该作者
原帖由 FreeGnu 于 2006-7-10 16:30 发表
>>*p = 1;
(*p)[0] = 1;


可是 int (*p)[2];是不是相当于 int a[2]; int *p = a;
而 可以这样*a =1;为a[0]赋值,怎么就不可以 *p=1; 了讷?

论坛徽章:
0
5 [报告]
发表于 2006-07-10 16:54 |只看该作者
>>可是 int (*p)[2];是不是相当于 int a[2]; int *p = a;
the two is not the same! p is a point , but a is the name of the array [2]

论坛徽章:
0
6 [报告]
发表于 2006-07-10 16:55 |只看该作者
原帖由 FreeGnu 于 2006-7-10 16:54 发表
>>可是 int (*p)[2];是不是相当于 int a[2]; int *p = a;
the two is not the same! p is a point , but a is the name of the array [2]


a也是个指针... *a是第一个元素的值,*(a+1)是第二个.

[ 本帖最后由 yangbo8481 于 2006-7-10 17:00 编辑 ]

论坛徽章:
0
7 [报告]
发表于 2006-07-10 17:08 |只看该作者
>>a也是个指针...
yes, it is. but here (*p) is not only point to &(*p)[0], but it also have anothe attribute :step up by 2*sizeof(int)

[ 本帖最后由 FreeGnu 于 2006-7-10 17:27 编辑 ]

论坛徽章:
0
8 [报告]
发表于 2006-07-10 17:19 |只看该作者
原帖由 FreeGnu 于 2006-7-10 17:08 发表
>>a也是个指针...
yes, it is. but here (*p) is not only point to &(*p)[0], but it also have anothe attribute :step up by 2*sizeof(int)
you can see the some articles about point and arra ...


我明白了,也就是说 p 装的只是数组的起始地址,*p=1;他不知道是分配一个字节还是两个字节来存它.而a具有了你所说的属性,a++会在地址上增加两个字节,用两个字节来存1,而如果是char a[2];a++ a增加的地址也只会是一个字节,用一个字节来存1.

论坛徽章:
0
9 [报告]
发表于 2006-07-10 20:45 |只看该作者
原帖由 FreeGnu 于 2006-7-10 16:30 发表
>>*p = 1;
(*p)[0] = 1;



sorry, it is wrong

论坛徽章:
0
10 [报告]
发表于 2006-07-12 21:24 |只看该作者
int (*p)[2];//并没有定义数组,而是只定义了一个指针p
*p = 1;//p没有向任何东西
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP