工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
楼主: Freedomer

有关calloc的使用

[复制链接]
 楼主| 发表于 2006-6-21 01:02 | 显示全部楼层
哦!听师兄讲一下胜过自己看书很多啊。
我还想问问怎样可以将在存储块里面的数据读入一个数组中啊?
我是这样写的(假设已经用指针p指向存储块里的10个数,且定义了一个数组a[]):
  for(i=o;i<10;i++)
   &a=*(p+i);
这个应该不行,我调试后发现真的不行,能说说原因吗?还有怎么可以解决呢?
    师兄不会嫌我烦吧,菜鸟嘛,有时候就是连简单的都搞不懂。
  不胜感激!!!
回复

使用道具 举报

发表于 2006-6-21 01:53 | 显示全部楼层
数组名本身就是一个地址……

  1. for(i=0;i<10;i++)a[i]=*p++;
复制代码
回复

使用道具 举报

发表于 2006-6-22 01:19 | 显示全部楼层
楼主的&a取的是a数组的头地址。
对数组的使用, 可以采用楼上的方法,也可以
for(i=0;i<10;i++)*a++=*p++;
回复

使用道具 举报

发表于 2006-6-22 14:39 | 显示全部楼层
上面搞错了,a是代表这个数组,指向的是数组的第一个元素,
其实&a也是指向数组第一个元素的地址。并不是a这个变量的地址。

[ 本帖最后由 hjack 于 2006-6-22 15:30 编辑 ]
回复

使用道具 举报

 楼主| 发表于 2006-6-23 16:51 | 显示全部楼层
原来是犯了一个很低级的错误,把一维数组与二维数组混淆了.....
不过经过两个星期的努力,终于把综合实验搞定了...
但,更郁闷的是...
迎来了课程设计,很难啊!!!!!师兄们可以指点指点吗?
回复

使用道具 举报

发表于 2006-6-23 17:19 | 显示全部楼层
LZ应该自己先构思,然后编码,过程中有什么心得体会和大家分享下。有问题,大家讨论下。

[ 本帖最后由 powerwind 于 2006-6-23 17:42 编辑 ]
回复

使用道具 举报

发表于 2006-6-24 23:52 | 显示全部楼层
原帖由 hjack 于 2006-6-22 14:39 发表
上面搞错了,a是代表这个数组,指向的是数组的第一个元素,
其实&a也是指向数组第一个元素的地址。并不是a这个变量的地址。


补充一点:
a和&a都是指向指向数组第一个元素的地址,但意义不同。
a是数组的首地址,即a[0]的地址,&a是数组a的首地址。这个说起来比较难,举个例子吧。


  1. int main()
  2. {
  3.     int a[3]={1,2,3};
  4.     int *p=a+1;
  5.     int *q=(int*)(&a+1);
  6.     printf("%d,%d\n",*(p-1),*(q-1));
  7.     system("pause");
  8.     return 0;
  9. }
复制代码

输出结果是:1,3
a+1指向”2“, (&a+1)指向a数组尽头的下一个地址(这个用二维数组来解释,如b[2][2],&b[0]+1就指向了b[1][0]的地址),减一后指向a数组的最后一个元素3
回复

使用道具 举报

 楼主| 发表于 2006-6-26 01:29 | 显示全部楼层
有一个问题:怎样可以输出汉字???
回复

使用道具 举报

发表于 2006-6-26 11:24 | 显示全部楼层
只是因为用TC的关系吧?
printf("汉字");
回复

使用道具 举报

 楼主| 发表于 2006-6-26 17:10 | 显示全部楼层
帮忙看看我这个程序为什么不能用exit退出啊?
CODE:#include<stdio.h>
#include<stdlib.h>
void main()
{char t;
scanf("%c",&t);
if(t=='N'||'n')
   exit(0);
  else printf("___\n");
getch();
}[Copy to clipboard]
怎样用exit退出程序啊?
回复

使用道具 举报

发表于 2006-6-26 17:52 | 显示全部楼层
if(t=='N'||t=='n')
   exit(0);
回复

使用道具 举报

 楼主| 发表于 2006-6-26 20:38 | 显示全部楼层
哦!!!
但我改为下面的时候
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. void main()
  4. {char c,d;
  5. printf("Do you want to enter?(Y/N)\n");
  6. scanf("%c",&c);
  7. if(c=='N'||c=='n')
  8.    exit(0);
  9. if(c=='Y'||c=='y')
  10.    clrscr();
  11. printf("Do you want to open a file?(Y/N)");
  12. scanf("%c",&d);
  13. if(d=='N'||d=='n')
  14.    exit(0);
  15. if(d=='Y'||d=='y')
  16. printf("%c",d);
  17. getch();
  18. }
复制代码

为什么第二次无论按哪一个字母他都自己退出啊?我的设想是输入(N/n),才退出啊,应该怎么该???
回复

使用道具 举报

发表于 2006-6-26 21:17 | 显示全部楼层
it goes to the end of the code.

so you should have a loop.
回复

使用道具 举报

发表于 2006-6-26 21:17 | 显示全部楼层

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. int main()
  4. {char c,d;
  5. printf("Do you want to enter?(Y/N)\n");
  6. scanf("%c",&c);
  7. if(c=='N'||c=='n')
  8.    exit(0);
  9. if(c=='Y'||c=='y')
  10. printf("  clrscr();\n");
  11. fflush(stdin);  
  12. printf("Do you want to open a file?(Y/N)");
  13. scanf("%c",&d);
  14. if(d=='N'||d=='n')
  15.    exit(0);
  16. if(d=='Y'||d=='y')
  17. printf("Open file: %c",d);
  18. getch();
  19. return 0;
  20. }
复制代码
回复

使用道具 举报

发表于 2006-6-26 21:29 | 显示全部楼层
原帖由 Freedomer 于 2006-6-26 20:38 发表
为什么第二次无论按哪一个字母他都自己退出啊?我的设想是输入(N/n),才退出啊,应该怎么该???


what does your 'second time' mean??
回复

使用道具 举报

发表于 2006-6-26 21:33 | 显示全部楼层
upstair,do you have any compiler?
Just compile and execute it!
回复

使用道具 举报

发表于 2006-6-26 21:46 | 显示全部楼层
no.i have not any compiler now.

i think i know why does it happen.

you enter a character at the first time. and it receives the character you enter,so it works.

but you have not received the enter key. At the second time, the enter key will be the character you enter,no metter what you enter actually.

to  solve it , use getch() after scanf() to receive the enter key.
回复

使用道具 举报

 楼主| 发表于 2006-6-26 22:00 | 显示全部楼层
哇!
版主们!你们不要耍我好不好?!突然讲英语!我怎么看得明白啊?简直是要命!
回复

使用道具 举报

发表于 2006-6-26 22:00 | 显示全部楼层
You have said that before.
See also C/C++常见问题
回复

使用道具 举报

 楼主| 发表于 2006-6-26 22:12 | 显示全部楼层
原帖由 hjack 于 2006-6-26 21:46 发表
no.i have not any compiler now.

i think i know why does it happen.

you enter a character at the first time. and it receives the character you enter,so it works.

but you have not received t ...

是不是说错了?!:L
不是应该加getchar()吗?
我加上d=getcher();就可以了
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-15 07:18

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表