C:我能';t显示数组[代码块]的内容
我有这个代码可以将数组temp复制到数组a 我不知道为什么它一直显示值的地址。。。而不是价值观C:我能';t显示数组[代码块]的内容,c,arrays,pointers,C,Arrays,Pointers,我有这个代码可以将数组temp复制到数组a 我不知道为什么它一直显示值的地址。。。而不是价值观 #include <stdio.h> #include <stdio.h> #define maxLength 14 #include <conio.h> #include <string.h> typedef short int *set; void copyarr(set a,set temp){ int i; a=(int*)mallo
#include <stdio.h>
#include <stdio.h>
#define maxLength 14
#include <conio.h>
#include <string.h>
typedef short int *set;
void copyarr(set a,set temp){
int i;
a=(int*)malloc(maxLength*sizeof(short int));
for(i=0;i<maxLength;i++)
a[i]=temp[i];
}
int main(){
int i;
set a,temp;
temp=(int*)malloc(maxLength*sizeof(short int));
for(i=0;i<maxLength;i++)
temp[i]=i+10;
copyarr(a,temp);
for(i=0;i<maxLength;i++)
printf("%d ",a[i]);
}
#包括
#包括
#定义最大长度14
#包括
#包括
typedef short int*集;
无效copyarr(设置a、设置温度){
int i;
a=(int*)malloc(maxLength*sizeof(short int));
对于(i=0;i
这对调用者没有任何影响。在你的代码中a
是一个指针,你所做的一切就是更改指针的本地副本。一天结束时,在main
a
中仍然没有指向任何内容。这个问题在本文中得到了很好的讨论
造成这种混淆的部分原因是,将a
是typedef
后面的指针这一事实隐藏起来。您可以像指针一样使用它,并依赖于它是指针这一事实,但您隐藏了此信息。只有在调用方真正不关心实际类型时,才应该使用typedef。typedef short int*set;
typedef short int *set;
set copyarr(set temp){
int i;
set b;
b=(set)malloc(maxLength*sizeof(short int));
for(i=0;i<maxLength;i++)
b[i]=temp[i];
return b;
}
int main(){
int i;
set a,temp;
temp=(set)malloc(maxLength*sizeof(short int));
for(i=0;i<maxLength;i++)
temp[i]=i+10;
a = copyarr(temp);
for(i=0;i<maxLength;i++)
printf("%d ",a[i]);
free(a);
free(temp);
return 0;
}
设置copyarr(设置温度){
int i;
b组;
b=(set)malloc(maxLength*sizeof(short int));
对于(i=0;i#包括
#包括
#定义最大长度14
typedef short int*集;
无效copyarr(设置*a,设置温度){
int i;
*a=malloc(maxLength*sizeof(short int));
对于(i=0;i这不是真正的代码,我正在处理一个大项目,不需要每次都键入short int set[]
,所以我只想使用set,所以我如何在仍然使用typedef的情况下做到这一点,但是当我在它显示的函数中执行printf时!为什么在main()中)它不会!我返回了指针!@user3225284,因为main
没有看到更改。那么我如何才能让main看到此更改?我阅读了常见问题解答,并喜欢所显示的内容。我如何不指定B就可以在main中显示,我想在main中使用a显示。main中没有B
。只有a
。我明白,但还有其他事情要做吗你是说不做a=copyarr(temp)吗?
a=(int*)malloc(maxLength*sizeof(short int));
typedef short int *set;
set copyarr(set temp){
int i;
set b;
b=(set)malloc(maxLength*sizeof(short int));
for(i=0;i<maxLength;i++)
b[i]=temp[i];
return b;
}
int main(){
int i;
set a,temp;
temp=(set)malloc(maxLength*sizeof(short int));
for(i=0;i<maxLength;i++)
temp[i]=i+10;
a = copyarr(temp);
for(i=0;i<maxLength;i++)
printf("%d ",a[i]);
free(a);
free(temp);
return 0;
}
typedef short int *set;
void copyarr(set a, set temp){
int i;
for(i=0;i<maxLength;i++)
a[i]=temp[i];
return;
}
int main(){
int i;
set a,temp;
temp=(set)malloc(maxLength*sizeof(short int));
a=(set)malloc(maxLength*sizeof(short int));
for(i=0;i<maxLength;i++)
temp[i]=i+10;
copyarr(a, temp);
for(i=0;i<maxLength;i++)
printf("%d ",a[i]);
free(a);
free(temp);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#define maxLength 14
typedef short int *set;
void copyarr(set *a, set temp){
int i;
*a=malloc(maxLength*sizeof(short int));
for(i=0;i<maxLength;i++)
(*a)[i]=temp[i];
}
int main(){
int i;
set a,temp;
temp = malloc(maxLength*sizeof(short int));
for(i=0;i<maxLength;i++)
temp[i]=i+10;
copyarr(&a,temp);
for(i=0;i<maxLength;i++)
printf("%d ",a[i]);
return 0;
}