我有这个代码把数组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*)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]);
}

编辑
更新的代码:仍然得到相同的结果,我做了FAQ链接中显示的事情
#include <stdio.h>
#include <stdio.h>
#define maxLength 14
#define maxSetLength 129
#include <conio.h>
#include <string.h>

typedef short int *set;

int *copyarr(set a,set temp){
    int i;
a=(int*)malloc(maxLength*sizeof(short int));
for(i=0;i<maxLength;i++)
    a[i]=temp[i];
return &a;
}

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]);
}

最佳答案

#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;
}

关于c - C:我无法显示数组的内容[CodeBlocks],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21294012/

10-10 00:56
查看更多