2010-05-11 2 views
0

구조, 기본 및 함수로 구성된 코드가 아래에 있습니다. 이 함수는 특정 값을 갖는 두 개의 매개 변수를 표시하며, 두 매개 변수는 모두 같은 구조를 가리 킵니다.하나의 함수에서 동일한 구조를 가리키는 두 개의 매개 변수를 사용하는 방법은 무엇입니까?

나는 다음과 같은 코드에 두 번째 매개 변수를 추가하는 방법을 잘 모릅니다 문제 :

이 코드 사용 (동일한 기능 IN)가 새 매개 변수 test_2를 추가 할 필요가
#include<stdio.h> 

#define first 500 
#define sec 500 


struct trial{ 
    int f; 
    int r; 
    float what[first][sec]; 
}; 

int trialtest(trial *test); 

main(){ 
    trial test; 
    trialtest(&test); 
} 

int trialtest(trial *test){ 
    int z,x,i; 
    for(i=0;i<5;i++){ 
     printf("%f,(*test).what[z][x]); 
    } 
    return 0; 
} 

:

for(i=0;i<5;i++){ 
     printf("%f,(*test_2).what[z][x]); 

int trialtest(trial *test)은 어떻게 변경됩니까? 주에서 어떻게 바뀌나요?

는 나는이 같은뿐만 아니라 test_2를 선언해야한다고 알고

trial test,test_2; 

그러나 함수의 주소를 전달하는 방법에 대한? 나는 그것을 바로 편집 할 필요가 없다?

trialtest(&test); --- This will remain the same ? 

그래서,

감사합니다 .. 저도 같은 기능을 모두 테스트와 같은 구조를 가리키는 매개 변수로 test_2 사용하는 것이 방법을 말해주십시오! 더 자세한 설명이 필요하면 알려주십시오.

+0

동일한 구조를 가리키는 두 개의 매개 변수를 사용하는 함수가있는 이유에 대해 혼란 스럽습니다. 뭔가 이상한 점이 있습니다. 왜 이럴 필요가 있니? – FrustratedWithFormsDesigner

+0

'test_2'를'struct trial '으로 선언하면 같은 구조체를 가리 키지 않고 새로운 구조체를 생성 할 수 있습니다. 나는 당신이 * 매우 혼란 스럽다고 생각합니다, 그리고 당신은 왜 당신이 처음부터이 모든 것이 필요하다고 생각하는지 설명함으로써 시작해야합니다. –

+0

우리 프로그램에서 두 개의 행렬 파일을 가져 와서 표시합니다.이 함수는 두 행렬 중 하나를 표시합니다. 두 행렬의 내용은 같은 구조로 저장됩니다. – NLed

답변

1

저는 이것이 숙제라고 생각합니다. 그래서 당신이해야 할 일에 대한 아이디어를 줄 수있는 다른 기능을 작성하겠습니다. trail_test 매개 변수 목록을 변경하고 싶지 않으므로 비슷한 매개 변수 목록을 사용했습니다.

struct thing { 
    /* put some stuff here */ 
}; 
typedef struct thing thing; /* because this is C, not C++ */ 

int how_many_things(thing * thing_list); 

int main(void) { 
    int i; 
    thing * a; 
    int count_init = random(); /* let's surprise ourselves and make a random number of these */ 
    count_init %= 128; /* but not too many or it might not work at all */ 

    a = malloc(count_init*sizeof(things)+1); 
    for (i = 0; i < count_init; i++) { 
     thing_init(&(a[i])); 
    } 
    make_illegal_thing(&(a[count_init])); /* like '\0' at the end of a string */ 

    printf("There are %i things in the list\n", how_many_things(a)); 

    return 0; 
} 

/* This is very similar to strlen */ 
int how_many_things(thing * a) { 
    int count = 0; 
    while (is_legal_thing(a)) { 
     a++; 
     count++; 
    } 
    return count; 
} 
관련 문제