편집 기록

편집 기록
  • 프로필 편집요청빌런님의 편집
    날짜2020.04.07

    함수매개변수 double배열 요소변경


    int main(void)
    {
    
        int a[2];
        double b[2];
    
        printf("첫 번째 사람 나이, 키 : \n");
        scanf("%d%lf", &a[0], &b[0]);
    
        printf("두 번째 사람 나이, 키 : \n");
        scanf("%d%lf", &a[1], &b[1]);
    
        swap("int", &a, &b);
        swap("double", &a, &b);
    
        printf("첫 번째 사람 나이, 키 : %d %.1lf\n", a[0], b[0]);
        printf("두 번째 사람 나이, 키 : %d %.1lf\n", a[1], b[1]);
    
        return 0;
    }
    
    void swap(char *type, int* a[], double* b[])
    {
    
        double *dtmp;
        int* tmp;
    
        if (strcmp(type, "int") == 0)
        {
            tmp = a[0];
            a[0] = a[1];
            a[1] = tmp;
        }
        else if (strcmp(type, "double") == 0)
        {       
            dtmp = b[0];
            b[0] = b[1];
            b[1] = dtmp;
        }
    }
    

    첫 번째 : 22 187.5 두 번째 : 44 165.4 이렇게 초기화 했을때 함수실행시키고 나서 나이값은 바뀌는데 키값은 바뀌지 않습니다. 어떤게 문제인지 모르겠습니다ㅠ.

  • 프로필 알 수 없는 사용자님의 편집
    날짜2020.04.06

    함수매개변수 double배열 요소변경


    int main(void) {

    int a[2];
    double b[2];
    
    printf("첫 번째 사람 나이, 키 : \n");
    scanf("%d%lf", &a[0], &b[0]);
    
    printf("두 번째 사람 나이, 키 : \n");
    scanf("%d%lf", &a[1], &b[1]);
    
    swap("int", &a, &b);
    swap("double", &a, &b);
    
    printf("첫 번째 사람 나이, 키 : %d %.1lf\n", a[0], b[0]);
    printf("두 번째 사람 나이, 키 : %d %.1lf\n", a[1], b[1]);
    
    return 0;
    

    }

    void swap(char type, int a[], double* b[]) {

    double *dtmp;
    int* tmp;
    
    if (strcmp(type, "int") == 0)
    {
        tmp = a[0];
        a[0] = a[1];
        a[1] = tmp;
    }
    else if (strcmp(type, "double") == 0)
    {       
        dtmp = b[0];
        b[0] = b[1];
        b[1] = dtmp;
    }
    

    }

    첫 번째 : 22 187.5 두 번째 : 44 165.4 이렇게 초기화 했을때 함수실행시키고 나서 나이값은 바뀌는데 키값은 바뀌지 않습니다. 어떤게 문제인지 모르겠습니다ㅠ.