2012-03-11 5 views
1

제발 도와주세요. 크기가 두 개의 정수 배열 인 을 합산해야하고 함수의 인수로 각 배열의 첫 번째 요소를 전달해야하는 함수가 있습니다. 이 닫힙니 까?정수 배열을 C에서 함수 매개 변수로 전달

12 void sumVect(int * v[], int * w[] ,int n) 
13 //adds the second vector to the first vector; the vectors v and w have exactly n components 
14 { 
15   int i; 
16   for(i=0;i<n;i++) 
17   { 
18     *v[i]+=*w[i]; 
19   } 
20 
21 } 

나는 전체 코드를 당신이 유형 int []을 전달하는

#include <stdio.h> 

void sumVect(int * v[], int * w[] ,int n) 
//adds the second vector to the first vector; the vectors v and w have exactly n components 
{ 
    int i; 
    for(i=0;i<n;i++) 
    { 
     *v[i]+=*w[i]; 
    } 

} 


int main() 
{ 
    int i,j; 
    int n = 4; //the size of a vector 
    int k = 5; //the number of vectors 
    int v[k][n]; //the vector of vectors 

    printf("How many vectors? k="); 
    scanf("%d",&k); 

    printf("How many components for each vector? n="); 
    scanf("%d",&n); 

    for(i=1;i<=k;i++) //read the content of each vector from the screen 
    { 
     printf("\nv%d |\n_____|\n",i); 

     for(j=0;j<n;j++) 
     { 
      printf("v%d[%d]=",i,j); 
      scanf("%d", &v[i][j]); 
     } 
    } 

    for(j=1;j<k;j++) 
    { 
     sumVect(&v[j], &v[j+1], n); 
     for(i=0;i<n;i++) 
     { 
      printf("%d",v[j][i]); 
     } 
    } 


    return 0; 
} 

답변

4

주의를 줄 것이다하지만 기능에 형식 매개 변수는 int *[]입니다. 따라서 귀하의 서명이 있어야한다 :

void sumVect(int v[], int w[] ,int n)

나 또한

void sumVect(int *v, int *w,int n)

당신이 모두 int * 또는 int []이 (달려있다

v[i] + w[i] 때문에 vw 같은 기능에에 액세스해야 공식적인 측광 장치에)

또한

당신이해야 전화 : v[j]로하는 동일한 주소로 평가됩니다 int [], &v[i]v[i]의 유형이기 때문에

sumVect(v[j], v[j+1], n);

또는

sumVect(&v[j], &v[j+1], n);

이입니다.

+2

덕분에 많이, 내가 당신에게 한 맥주 :)) – NiCU

+0

downvote 원인을 빚 완벽한 설명을?(참고, 오류 고정) – phoxis

+0

죄송합니다, 저는 기본 영어 사용자가 아니므로 downvote가 무슨 뜻입니까? – NiCU

1

이것은 함수 sumVect의 수정 된 버전입니다.

void sumVect(int * v, int * w ,int n) 
//adds the second vector to the first vector; the vectors v and w have exactly n components 
{ 
    int i; 
    for(i=0;i<n;i++) 
    { 
     v[i]+=w[i]; 
    } 

} 

하지만 전체 수정이 필요합니다. 이 코드를보고 :

여기
int n = 4; //the size of a vector 
int k = 5; //the number of vectors 
int v[k][n]; //the vector of vectors 

printf("How many vectors? k="); 
scanf("%d",&k); 

printf("How many components for each vector? n="); 
scanf("%d",&n); 

는 크기 5 × 2 - dimentional 배열의 할당이며, 다음 프로그램은 배열의 크기가 실제로해야 어떤 사용자에게 묻습니다. 가장 간단한 방법은 배열의 힙 할당을 사용하는 것입니다. 다음은 고정 코드입니다.

int n = 4; //the size of a vector 
int k = 5; //the number of vectors 
int **v = NULL; //the vector of vectors 

printf("How many vectors? k="); 
scanf("%d",&k); 

printf("How many components for each vector? n="); 
scanf("%d",&n); 
v = malloc(sizeof(int*), k); 
for(int i = 0; i < k; ++i) 
    v[i] = malloc(sizeof(int), n); 

마지막으로 비슷한 방식으로 할당 된 메모리를 확보해야하지만 숙제가되어야합니다.

+0

ok, 그건 어리석은 실수였습니다 예, 고마워요. – NiCU

1

배열과 포인터는 일반적으로 c에서 같은 것으로 처리됩니다. 배열 변수는 배열에 대한 포인터를 보유하고 [] 표기는 포인터에 대한 바로 가기입니다.

n은 부호없는 유형이어야합니다.

void sumVect(int * v, int * w, uint n) 
{ 
    while (n--) 
    { 
     *v++ += *w++; 
    } 
} 
+0

예,하지만 n은 각 벡터의 구성 요소 수를 나타냅니다. 문제의 설명에 따르면 동일한 수의 구성 요소가있는 두 개의 배열을 추가해야한다고 예 : v1 = 1 1 1 및 v2 = 2 2 2) – NiCU

관련 문제