哦!听师兄讲一下胜过自己看书很多啊。
我还想问问怎样可以将在存储块里面的数据读入一个数组中啊?
我是这样写的(假设已经用指针p指向存储块里的10个数,且定义了一个数组a[]):
for(i=o;i<10;i++)
&a=*(p+i);
这个应该不行,我调试后发现真的不行,能说说原因吗?还有怎么可以解决呢?
师兄不会嫌我烦吧,菜鸟嘛,有时候就是连简单的都搞不懂。
不胜感激!!!
数组名本身就是一个地址……
for(i=0;i<10;i++)a=*p++;
楼主的&a取的是a数组的头地址。
对数组的使用, 可以采用楼上的方法,也可以
for(i=0;i<10;i++)*a++=*p++;
上面搞错了,a是代表这个数组,指向的是数组的第一个元素,
其实&a也是指向数组第一个元素的地址。并不是a这个变量的地址。
[ 本帖最后由 hjack 于 2006-6-22 15:30 编辑 ]
原来是犯了一个很低级的错误,把一维数组与二维数组混淆了.....
不过经过两个星期的努力,终于把综合实验搞定了...
但,更郁闷的是...
迎来了课程设计,很难啊!!!!!师兄们可以指点指点吗?
LZ应该自己先构思,然后编码,过程中有什么心得体会和大家分享下。有问题,大家讨论下。
[ 本帖最后由 powerwind 于 2006-6-23 17:42 编辑 ]
原帖由 hjack 于 2006-6-22 14:39 发表
上面搞错了,a是代表这个数组,指向的是数组的第一个元素,
其实&a也是指向数组第一个元素的地址。并不是a这个变量的地址。
补充一点:
a和&a都是指向指向数组第一个元素的地址,但意义不同。
a是数组的首地址,即a的地址,&a是数组a的首地址。这个说起来比较难,举个例子吧。
int main()
{
int a={1,2,3};
int *p=a+1;
int *q=(int*)(&a+1);
printf("%d,%d\n",*(p-1),*(q-1));
system("pause");
return 0;
}
输出结果是:1,3
a+1指向”2“, (&a+1)指向a数组尽头的下一个地址(这个用二维数组来解释,如b,&b+1就指向了b的地址),减一后指向a数组的最后一个元素3
有一个问题:怎样可以输出汉字???
只是因为用TC的关系吧?
printf("汉字");
帮忙看看我这个程序为什么不能用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();
}
怎样用exit退出程序啊?
if(t=='N'||t=='n')
exit(0);
哦!!!
但我改为下面的时候#include<stdio.h>
#include<stdlib.h>
void main()
{char c,d;
printf("Do you want to enter?(Y/N)\n");
scanf("%c",&c);
if(c=='N'||c=='n')
exit(0);
if(c=='Y'||c=='y')
clrscr();
printf("Do you want to open a file?(Y/N)");
scanf("%c",&d);
if(d=='N'||d=='n')
exit(0);
if(d=='Y'||d=='y')
printf("%c",d);
getch();
}
为什么第二次无论按哪一个字母他都自己退出啊?我的设想是输入(N/n),才退出啊,应该怎么该???
it goes to the end of the code.
so you should have a loop.
#include<stdio.h>
#include<stdlib.h>
int main()
{char c,d;
printf("Do you want to enter?(Y/N)\n");
scanf("%c",&c);
if(c=='N'||c=='n')
exit(0);
if(c=='Y'||c=='y')
printf("clrscr();\n");
fflush(stdin);
printf("Do you want to open a file?(Y/N)");
scanf("%c",&d);
if(d=='N'||d=='n')
exit(0);
if(d=='Y'||d=='y')
printf("Open file: %c",d);
getch();
return 0;
}
原帖由 Freedomer 于 2006-6-26 20:38 发表
为什么第二次无论按哪一个字母他都自己退出啊?我的设想是输入(N/n),才退出啊,应该怎么该???
what does your 'second time' mean??
upstair,do you have any compiler?
Just compile and execute it!
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.
tosolve it , use getch() after scanf() to receive the enter key.
哇!
版主们!你们不要耍我好不好?!突然讲英语!我怎么看得明白啊?简直是要命!
You have said that before.
See also C/C++常见问题
原帖由 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();就可以了