2016-03-25 123 views
-1

我似乎無法找到任何有關如何通過函數中的指針訪問數組的元素的信息,我嘗試了多個答案,但他們都沒有爲我工作。如何通過C中的指針訪問矩陣數組的元素?

我的任務是下一個:用維度爲m x n的維度編寫一個程序,元素從0到9隨機生成。使用兩個新函數計算偶數元素的總和並計算等於零的元素數。

#include <stdio.h> 
#include <stdlib.h> 

void SumEven(int *a, int n, int m, int *sum){ 

} 

void EqualToZero(int *a, int n, int m, int *number){ 

} 

int main() 
{ 
    int** a; 
    int m, n, l, i, j, r, sum; 

    printf("Enter number of columns for matrix: "); 
    scanf("%d", &m); 

    printf("Enter number of rows for matrix: "); 
    scanf("%d", &n); 

    a = (int **) malloc(m*sizeof(int)); 

    for (l = 0 ; l < m ; l++){ 
     a[l] = (int **) malloc(n*sizeof(int)); 
    } 

    time_t t; 

    srand((unsigned)time(&t)); 

    printf("\n"); 
    printf("Your matrix is:\n"); 
    printf("\n"); 

    for(i = 0 ; i < m ; i++){ 
     for(j = 0 ; j < n ; j++){ 
      r = rand() % 10; 
      a[i][j] = r; 
      printf("%d ", r); 
     } 
     printf("\n"); 
    } 

    printf("\n"); 

    SumEven(&a, n, m); 

    return(0); 
} 

正如你可以在提供的代碼中看到我留下的那些功能空的,因爲我不知道如何通過矩陣給他們,並訪問他們的內容,所以我可以能打印我的結果。

而且我對於該功能的邏輯僞代碼本身是:

if(a[i][j] % 2 == 0) 
    printf("%d ", a[i][j]) 

if(a[i][j] == 0) 
    printf("%d ", a[i][j]) 

同樣功能的這些參數在我的任務是預定義的,所以我要跟着他們。

編輯:我也不知道我是否甚至將相同的矩陣傳遞給函數SumEven(&a, n, m);。我嘗試輸出矩陣的地址,並使用printf("%d", &a)來顯示main()SumEven()函數的地址。

+0

1)不要用C投malloc'與朋友'的結果! 2)「矩陣通常與二維數組同步,在你的代碼中沒有像這樣的構造,沒有一個可以作爲一個使用; 3)使用二維數組;這是直接的語法。 – Olaf

回答

0
This code may help. It does the following: 

1. For an arbitrary array of integers, sum the elements of the array 
    - using a pointer to the SUM function 

2. For an arbitrary array of integers, count the number of zero elements in 
    the array - using a pointer to the COUNTZERO function 


    #include <stdio.h> 
    #include <stdlib.h> 

    // sum the elements of the matrix 
    void sum(int* arr, int rows, int cols, int* result) 
    { 
     int sum = 0; 
     int i, j; 
     for (i = 0; i < rows; i++) { 
      for (j = 0; j < cols; j++) { 
       sum = sum + arr[i*cols + j]; 
      } 
     } 

     *result = sum; 
    } 

    // count the number of zero elements in the matrix 
    void countZero(int* arr, int rows, int cols, int* result) 
    { 
     int count = 0; 
     int i, j; 
     for (i = 0; i < rows; i++) { 
      for (j = 0; j < cols; j++) { 

       if (arr[i*cols + j] ==0) count = count + 1; 
      } 
     } 
     *result = count; 
    } 


    // arbitrary initialisation of 2D array of ints (force last entry of the array to equal zero - for testing purposes) 
    void init2D(int *arr, int rows, int cols) { 

     int i, j; 

     for (i = 0; i < rows; i++) { 
      for (j = 0; j < cols; j++) { 
       arr[i*cols + j] = 1; 
      } 
     } 

     // use this to test the countZero function 
     arr[(rows-1)*(cols-1)] = 0; 

    } 

    int main() { 

     int *array;  // will hold a 2D array of integers 
     int N = 10;  // arbitrary number of rows  
     int M = 5;  // arbitrary num cols 

     // 2D array of integers expressed as one "contiguous row" of memory 
     // make sure your indexing is correct when referenceing the array for (i,j)th element 
     array = (int*)malloc(sizeof(int)*N*M); 
     if (array != NULL) { 
      init2D(array, N, M); 
     } 

     // the function pointer 
     void(*general)(int*,int,int,int*); 

     // will contain the sum result 
     int sumAll = 0; 
     int* ptsumAll = &sumAll; 

     // make the function pointer point to the sum function 
     general = &sum; 

     // sum the contents of the array 
     general(array,N,M, ptsumAll); 
     printf("sum of array elements: %d\n", *ptsumAll); 

     // contains a count of the zero elements in the array 
     int count =0; 
     int* ptcount = &count; 

     // make the function pointer point to the count function 
     general = &countZero; 

     // count the number of zero element in the array 
     general(array, N, M,ptcount); 
     printf("number of zeros: %d\n", *ptcount); 

     free(array); 

     return 0; 
    } 

一些參考: https://www.cs.swarthmore.edu/~newhall/unixhelp/C_arrays.html http://www.cprogramming.com/tutorial/function-pointers.html

+0

感謝您花時間回覆。它的工作現在感謝@Rajeev – brajevicm

+0

歡迎你 - 很高興你解決了這個問題 – gps

-1
if((*(*(a + i) + j) % 2) == 0) 
    printf("%d", *(*(a + i) + j)) 


if(*(*(a + i) + j) == 0) 
    printf("%d", *(*(a + i) + j)) 

這就是你是如何做到的。

-1

下面是一個例子,給定3D陣列

int buffer[5][7][6]; 

的元件在位置[2][1][2]可以作爲buffer[2][1][2]*(*(*(buffer + 2) + 1) + 2)訪問。

Reference

0

這將是你的函數原型:

void SumEven(int **a, int n, int m,int *sum); 
void EqualToZero(int **a, int n, int m,int *number); 

因爲您撥打電話,則應該有一個雙指針(int **a)來接收它傳遞a(雙指針)。

呼叫:

SumEven(a, n, m,&sum); 
EqualToZero(a, n, m,&number); 

這是你可以訪問你的函數內部數組:

void SumEven(int **a, int n, int m,int *sum){ 
    int i,j,tsum=0; 
    for(i = 0 ; i < m ; i++){ 
     for(j = 0 ; j < n ; j++){ 
      if(a[i][j] % 2 == 0) 
      { 
       tsum+=a[i][j]; 
       printf("%d ",a[i][j]); 
      } 
     } 
    } 
    *sum=tsum; 
} 

也有是在此行a[l] = (int **) malloc(n*sizeof(int));( 'int** '到' int*' 錯誤分配),它應該是a[l] = (int *) malloc(n*sizeof(int));

+0

這個作品,但是當我調用函數來打印main()和SumEven的地址時,矩陣'a'的地址是不一樣的。是否有解決方法? – brajevicm

+0

地址是相同的(它們應該是)在這兩個函數中,我檢查了它們! –

+0

只做了很少的改變,它現在完美地工作,非常感謝您的幫助,只是多了一個問題。int ** a'和'int * a []' ?再一次感謝了很多,在過去的幾個小時裏我一直在說我的頭。 – brajevicm

0

我已添加評論以幫助您處理代碼。

#include <stdio.h> 
#include <stdlib.h> 

void SumEven(int *a, int n, int m, int *sum){ 
    //put this statement in 2 nested for loops of size n and m 
    if(a[i][j] % 2 == 0) 
     sum += a[i][j]; 
} 

void EqualToZero(int *a, int n, int m, int *number){ 
    //put this statement in 2 nested for loops of size n and m 
     if(a[i][j] == 0) 
      number++; 
} 

int main() 
{ 
int** a; 
int m, n, l, i, j, r, sum; 

printf("Enter number of columns for matrix: "); 
scanf("%d", &m); 

printf("Enter number of rows for matrix: "); 
scanf("%d", &n); 

a = (int **) malloc(m*sizeof(int)); 
//should be m*sizeof(int*) 

for (l = 0 ; l < m ; l++){ 
    a[l] = (int **) malloc(n*sizeof(int)); 
    //should be cast as (int*) 
} 

//I suggest you take look at declaring 2d arrays in C 
time_t t; 

srand((unsigned)time(&t)); 

printf("\n"); 
printf("Your matrix is:\n"); 
printf("\n"); 

for(i = 0 ; i < m ; i++){ 
    for(j = 0 ; j < n ; j++){ 
     r = rand() % 10; 
     a[i][j] = r; 
     printf("%d ", r); 
    } 
    printf("\n"); 
} 

printf("\n"); 

SumEven(&a, n, m); 
//need to pass &sum to this function. Also make sure it is initialized to 0 
//call EqualToZero() function with proper parameters. 
return(0); 
//return 0; not return(0); 
} 
+0

感謝您的快速回復。我改變了你所寫的所有內容,但是當我按照你的步驟操作時,仍然會在函數內部遇到一個錯誤'下標值既不是數組也不是向量'。 – brajevicm