2011-10-30 99 views
0

我試圖做一個結構,產生一個隨機矩陣,並得到「錯誤:預期的應該是」,「」,「,」,「或」屬性_â編譯時「。我如何纔能有效地工作?C noob,隨機矩陣結構創建

我猜想預期的錯誤通常是由錯別字造成的,但我沒有看到任何錯誤。

我對C非常陌生,所以指針和malloc對我來說很陌生。我非常感謝你的幫助。

/* It's called RandomMatrixMaker.c */ 

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

typdef struct { 
    char* name; 
    int MID; 
    int MRows; 
    int MCols; 
    long[][]* MSpace; 
} matrix; 

matrix makeRIDMatrix(char* name, int MID, int MRows, int MCols) { 
    matrix m; 
    static int i, j, r; 
    m.name = name; 
    m.MID = MID; 
    m.MRows = MRows; 
    m.MCols = MCols; 
    for (i=0; i<m.MRows; i++) { 
    for (j=0; i<m.MCols; j++) { 
     r = random(101); 
     *(m.MSpace[i][j]) = r; 
    } 
    } 
    return m; 
} 

int main(void) { 
    makeRIDMatrix("test", 1, 10, 10); 
    return 0; 
} 

回答

4

確實有一個錯字。你拼錯typedef

typdef struct { 

應該是:

typedef struct { 

編輯:

而且,沒有理由在這裏使用static

static int i, j, r; 

你可以只擺脫static修飾符。

int i, j, r; 
+0

謝謝你,現在,當然,我還有其他問題。如何在創建過程中由#rows和#cols定義的結構中有可變大小的數組? – PrckPgn

+3

這可能應該作爲一個單獨的問題。我對結構本身中的變量大小數組不熟悉。但你可以嘗試看看這些問題:(http://stackoverflow.com/questions/688471/variable-sized-struct-c),(http://stackoverflow.com/questions/1223876/how-to-create-一個結構與 - 雙可變大小陣列式-C)。 – Mysticial

1

關於你對「可變大小的數組」的問題,你可以有這樣的:

/* can stick this into your struct, this is just an example */ 
size_t rows, cols; 
long **matrix; 

/* set the values of rows, cols */ 

/* create the "array" of rows (array of pointers to longs) */ 
matrix = (long**)malloc(rows * sizeof(long*)); 

/* create the array of columns (array of longs at each row) */ 
for (i = 0; i < rows; i++) 
    matrix[i] = (long*)malloc(cols * sizeof(long)); 

/* ... */ 

/* free the memory at the end */ 
for (i = 0; i < rows; i++) 
    free(matrix[i]); 

free(matrix); 

然後,你可以訪問相同的陣列中的任何其他陣列的動態分配矩陣。

即。在第一行(第0行)和第四列(列3)設置元件以5:

matrix[0][3] = 5; 
3

如所提到的另一個海報,有一個錯字,但即使與該糾正,它不會編譯,由於到matrix.MSpace的定義。

讓我們從makeRIDMatrix()開始。你已經聲明瞭一個類型爲「matrix」的自動(堆棧)變量。在函數結束時,您返回該對象。雖然這是允許的,但這不可取。如果結構很大,則會不必要地複製大量數據。最好將指向矩陣的指針傳遞給makeRIDMatrix(),並使makeRIDMatrix()填充內容。

內循環中的測試是針對i的,但應該是針對j。

接下來,我們來看看「矩陣」的定義。 「空間」的定義是一團糟,甚至不會編譯。即使這樣做了,因爲您尚未定義行的長度,編譯器將無法計算數組中任何給定項的偏移量。你想不給排長度的二維數組,但你不能這樣做,在C.你可以在其他語言,但不是C.

有很多我可以指出,但我錯過了真正的觀點。真實的一點是:

C不是Java。

(這也不是解釋型語言,如JavaScript,PHP,Python和Ruby等這樣的一個。)

您不會獲得動態擴展數組;你不會自動分配內存;你不會收到未被引用的內存。

你需要的是更多的東西是這樣的:

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

typedef struct { 
    char* name; 
    int MID; 
    unsigned int MRows; 
    unsigned int MCols; 
    long *MSpace; 
} matrix; 

void makeRIDMatrix(matrix *pmx, char* name, int MID, 
        unsigned int MRows, unsigned int MCols) { 
    int i, j; 
    long *MSpace = malloc(sizeof(*MSpace)*MRows*MCols); 
    if (MSpace == NULL) { 
    return; 
    } 
    pmx->name = name; 
    pmx->MID = MID; 
    pmx->MRows = MRows; 
    pmx->MCols = MCols; 
    pmx->MSpace = MSpace; 

    srandom((unsigned int)time(NULL)); 
    for (i=0; i<MRows; i++) { 
    for (j=0; i<MCols; j++) { 
     long int r = random() % 101L; 
     *(MSpace++) = r; 
    } 
    } 
} 

inline long * item_addr(const matrix *pmx, 
         unsigned int row, unsigned int col) { 
    if (pmx == NULL || pmx->MSpace == NULL 
     || row >= pmx->MRows || col >= pmx->MCols) { 
    return NULL; 
    } 
    return &(pmx->MSpace[row * pmx->MCols + col]); 
} 

long get_item(const matrix *pmx, unsigned int row, unsigned int col) { 
    long *addr = item_addr(pmx, row, col); 
    return addr == NULL ? 0L : *addr; 
} 

void set_item(matrix *pmx, 
       unsigned int row, unsigned int col, 
       long val) { 
    long *addr = item_addr(pmx, row, col); 
    if (addr != NULL) { 
    *addr = val; 
    } 
} 

int main(void) { 
    matrix m; 
    makeRIDMatrix(&m, "test", 1, 10, 10); 
    return 0; 
} 

注意這裏的幾件事情。首先,爲了提高效率,我把它看作是一維的。爲了安全起見,所有後續的獲取/設置數組項目應該通過getter/setter函數完成。其次,一個隱藏的討厭:makeRIDMatrix()已經使用了malloc()來分配內存 - 但它將作爲調用函數(或其後繼者)的工作來釋放()分配的指針,當它完成時。

第三,我將行/列變量更改爲無符號整數 - 在定義具有負指數的數組時沒有多大意義!第四:小錯誤檢查。例如,makeRIDMatrix()既不知道也不關心參數值是否合理(例如,矩陣指針未檢查NULL值)。這是對學生的練習。

第五,我已經修復了你的隨機數的使用 - 在時尚之後。對學生的另一個練習:爲什麼我這樣做不是很好的做法?

但是 - 所有這些都沒有實際意義。你需要讓自己掌握一本好的C語言教材,或者一個好的在線課程,並通過例子來學習。你在這裏給出的代碼表明你正在衝擊你的體重,並且在進入那個環之前你需要開發更多的C肌肉!

+0

非常感謝您的深思。目前我正在參加一個嵌入式編程課程,並且能夠在詳細的apis上表現良好,但是我自己感覺身高4英寸。將您的建議放在心上。 – PrckPgn