注册 登录
编程论坛 C语言论坛

函数传递数组问题,代码不知道哪里错,求教大佬

Pa407 发布于 2023-09-06 11:34, 659 次点击
# include <stdio.h>


int aabbcc(int a,int b,int c){
       int aaa[12];
        int shu1,shunni,shu2;
    shu1=c;
    shu2=b;
    if(a==1) shunni=1;
    if(a==3) shunni=1;
    if(a==2) shunni=2;
    if(a==4) shunni=2;

    int i;
    if(shunni==1){
        for(i=0;i<12;i++){
            aaa[i]=(shu1+120-shu2*10+10*i)%120;   
                          }        
                  }
    else if(shunni==2){
        for(i=0;i<12;i++){
            aaa[i]=(shu1+10*shu2+120-i*10)%120;
                         }
                      }
    for(i=0;i<12;i++){
    printf("%d ",aaa[i]);}
    printf("\n");
    return *aaa;
}

int main(){
int a, b, c;
int aaa[12];   
a=1;
b=2;
c=3;
aaa[12]=aabbcc(a,b,c);
int i;
    for(i=0;i<12;i++){
    printf("%d ",aaa[i]);}
return 0;   
}
7 回复
#2
forever742023-09-06 12:09
根据上下文,return的*aaa相当于aaa[0]
你不能return一个数组。
#3
forever742023-09-06 12:12
程序代码:
int aabbcc(int a,int b,int c,int *aaa)
...
int main()
...
aabbcc(a,b,c,aaa);
...
#4
Pa4072023-09-06 13:10
你的代码,结果还是不对。编译都通不过。
#5
Pa4072023-09-06 13:17
成功了。谢谢forever74.

# include <stdio.h>


int aabbcc(int a,int b,int c,int aaa[]){
      
        int shu1,shunni,shu2;
    shu1=c;
    shu2=b;
    if(a==1) shunni=1;
    if(a==3) shunni=1;
    if(a==2) shunni=2;
    if(a==4) shunni=2;

    int i;
    if(shunni==1){
        for(i=0;i<12;i++){
            aaa[i]=(shu1+120-shu2*10+10*i)%120;   
                          }        
                  }
    else if(shunni==2){
        for(i=0;i<12;i++){
            aaa[i]=(shu1+10*shu2+120-i*10)%120;
                         }
                      }
    for(i=0;i<12;i++){
    printf("%d ",aaa[i]);}
    printf("\n");
    return *aaa;
}

int main(){
int a, b, c;
int aaa[12];   
a=1;
b=2;
c=3;
aaa[12]=aabbcc(a,b,c,aaa);
int i;
    for(i=0;i<12;i++){
    printf("%d ",aaa[i]);}
return 0;   
}
#6
rjsp2023-09-06 15:32
aaa[12]=aabbcc(a,b,c,aaa);
这还是错误的
看不懂你这个函数是想完成什么功能
#7
forever742023-09-06 19:49
不问内容只看语法
程序代码:
# include <stdio.h>
void aabbcc(int a, int b, int c, int aaa[])
{
    int shu1, shunni, shu2;
    shu1 = c;
    shu2 = b;
    if (a == 1) shunni = 1;
    if (a == 3) shunni = 1;
    if (a == 2) shunni = 2;
    if (a == 4) shunni = 2;

    int i;
    if (shunni == 1) {
        for (i = 0; i < 12; i++) {
            aaa[i] = (shu1 + 120 - shu2 * 10 + 10 * i) % 120;
        }
    }
    else if (shunni == 2) {
        for (i = 0; i < 12; i++) {
            aaa[i] = (shu1 + 10 * shu2 + 120 - i * 10) % 120;
        }
    }
    for (i = 0; i < 12; i++) {
        printf("%d ", aaa[i]);
    }
    printf("\n");
}

int main() {
    int a, b, c;
    int aaa[12];
    a = 1;
    b = 2;
    c = 3;
    aabbcc(a, b, c, aaa);
    int i;
    for (i = 0; i < 12; i++) {
        printf("%d ", aaa[i]);
    }
    return 0;
}
#8
AZhengYe2023-09-08 12:48
数组下标越界。
int aaa[12];        //是aaa[0]-aaa[11]
aaa[12]=aabbcc(a,b,c,aaa);    //没有aaa[12],下标越界了
1