|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 lengdao 于 2019-5-30 21:46 编辑
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int *p=NULL;
int *q=NULL;
int i;
p=(int *)calloc(2,sizeof(int));
for(i=0;i<2;i++)
{
printf("请输入第%d个数:\n",i+1);
scanf("%d",&p[i]);
}
q=(int *)calloc(4,sizeof(int));
memcpy(q,p,2);
free(p);
printf("%d %d %d %d\n",q[0],q[1],q[2],q[3]);
free(q);
return 0;
}
如果输入数7和9,则输出结果是7 0 0 0 这是为什么呀
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int main()
- {
- int *p=NULL;
- int *q=NULL;
- int *tmp; // 为保证p的首地址不变,所以用tmp来移动 p
- int i;
- p= (int *)calloc(2,sizeof(int));
- tmp = p;
- for(i=0;i<2;i++)
- {
- printf("请输入第%d个数:\n",i+1);
- scanf("%d",tmp); // 你的编译器很神奇 加&能工作
- tmp++; // 移动到下一个地址接收用户输入
- }
- q=(int *)calloc(4,sizeof(int));
- memcpy(q,p,2*sizeof(int)); // memcpy 是按字节来计算的,所以2*sizeof(int)
- free(p);
- printf("%d %d %d %d\n",q[0],q[1],q[2],q[3]);
- free(q);
- return 0;
- }
复制代码
|
|