2012-01-12 96 views
0

具有混合數據類型的指針我試圖定義像這樣 我有矩陣用C

typedef struct _struct { 
    int name; 
    int data; 
} myDataType; 

然後我定義的矩陣

int **myMatrix = calloc(size,sizeof(int*)); 
for() 
    // allocate rows except last index 
myMatrix[last_index_in_matrix] = calloc(1,sizeof(myDataType)); 

的問題是,結構的矩陣的我不能訪問myMatrix [last_index] .data它說,也試過 - >(我真的不知道什麼時候用什麼)

request for member ‘data’ in something not a structure or union 

我在做什麼錯?我應該發佈實際的代碼嗎?如果這種方法是不可能的,我可以得到不同的建議?

更新:我會再說一遍,矩陣都是int,我只是想讓最後一行指向那個結構,有些評論沒有考慮到這一點。這就是我在我的例子中聲明的方式。

+3

首先,所有的'int'指針是什麼?如果你想要一個指向'myDataType'的指針數組,然後*分配一個指向'myDataType'的指針數組,並將它們存儲在正確的結構中。那麼也許你的IDE可以給你一些提示。 :) – 2012-01-12 18:35:38

+0

什麼是「member'id'」?我認爲我們需要更多的上下文(=代碼)。 – marcelnijman 2012-01-12 18:36:44

+0

@marcelnijman這就是錯誤看起來像請求會員'我試圖訪問'在某些不是結構或聯盟 – andrei 2012-01-12 18:51:34

回答

2

讓我們從簡單的開始:

你要創建的myDataType矩陣。如果你知道在編譯時的行和列的數量,你可以簡單地把它聲明爲

myDataType matrix[ROWS][COLS]; 

如果你需要動態地分配它,你會做這樣的事情:

myDataType **matrix; 

matrix = calloc(rows, sizeof *matrix); 
if (matrix) 
{ 
    size_t r; 
    for (r = 0; r < rows; r++) 
    { 
    matrix[r] = calloc(cols, sizeof *matrix[r]); 
    } 
} 

無論哪種方式在th元素的i '日和j' 訪問結構成員,你會寫:

matrix[i][j].name = ...; 
matrix[i][j].data = ...; 

編輯

啊,現在我明白了。

不要這樣做。

不能保證指向struct類型的指針與指向int的指針的大小和表示形式相同。他們在大多數常見架構上做,但這不是你可以依賴的。如果他們不這樣做,那麼你將遇到運行時問題。

從設計角度來看,這隻會讓我發癢;如果你需要的是結構與矩陣相關聯,創建一個新的聚集類型明確這樣做的:

struct composite 
{ 
    int **matrix; 
    struct myData data; 
}; 

這會讓生活,當您需要騰出矩陣,以及更容易。

FWIW,做你想要的東西,你就需要搞一些轉換,如

(struct myData *) myMatrix[last_index] = malloc(sizeof (struct myData)); 

((struct myData *) myMatrix[last_index])->data = ...; 

,但正如我上面所說,如果指針類型不兼容,轉換可能會導致運行時錯誤。不要這樣做。壞juju。

+0

不,我想創建一個int矩陣與最後一行指向這樣的結構 – andrei 2012-01-12 23:17:23

+0

@andrei:請參閱編輯。 – 2012-01-12 23:51:35

+0

謝謝,但不知何故,我必須這樣做。我有很多數據從文件中拉到不同的內存區域,我必須以某種方式跟蹤它 – andrei 2012-01-13 07:21:35

1

你應該把指針轉換爲相應的類型:

int val = ((MyDataType *)myMatrix[last_index_in_matrix])->data 

否則你試圖找到一個名爲dataint *這當然無法做到的領域。或者,將myMatrix聲明爲MyDataType **

+0

矩陣必須包含數字,這就是爲什麼我將它聲明爲int,最後一個指針用於存放矩陣所屬的位置等信息。 – andrei 2012-01-12 18:41:35

+0

val是誰?我需要將數據存入矩陣中,如果它不存在 – andrei 2012-01-12 18:43:15

0

以下是你應該做的分配使用動態malloc()

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

#define ROW 10  
#define COL 10 

int main(void) 
{ 
    char **arr = NULL; 
    int i; 

    if ((arr = malloc(sizeof(char *) * ROW)) == NULL) { 
     printf("unable to allocate memory \n"); 
     return -1; 
    } 

    for (i=0; i<ROW ; i++) { 
     if ((arr[i] = malloc(sizeof(char) * COL)) == NULL) { 
      printf("unable to allocate memory \n"); 
      return -1; 
     } 
    } 

    /* here you can use arr[][] */ 

    for (i=0; i<ROW ; i++) 
     free(arr[i]); 
    free(arr); 

    return 0; 
} 

OTOH,訪問下面的結構成員變量二維矩陣你有以下幾種方式

typedef struct _struct { 
    int name; 
    int data; 
} myDataType; 

選項#1

/* creating auto variable of type myDataType which gets allocated in stack */ 
myDataType mdt; 

/* to access the member variables, you need to do the following */ 
mdt.data = 10 

選項#2

/* creating a pointer variable of type myDataType 
    and allocating memory from heap using malloc() */ 
myDataType *pmdt = NULL; 

if ((pmdt = malloc(sizeof(myDataType) * numberofelements)) == NULL) { 
    printf("unable to allocate memory \n") 
    return -1; 
} 

/* to access the member variables using the pointer, you need to do the following */ 
pmdt->data = 100; 

/* finally you should free the memory allocated using malloc() using free() */ 
free(pmdt); 

希望它有幫助!


根據OP作者的要求,將上述兩個程序合併爲一個。

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

#define ROW 10  
#define COL 10 

typedef struct _struct { 
    int data; 
} myDataType; 

int main(void) 
{ 
    myDataType **arr = NULL; 
    int i, j; 
    int val = 0; 

    if ((arr = malloc(sizeof(myDataType *) * ROW)) == NULL) { 
     printf("unable to allocate memory \n"); 
     return -1; 
    } 

    for (i=0; i<ROW ; i++) { 
     if ((arr[i] = malloc(sizeof(myDataType) * COL)) == NULL) { 
      printf("unable to allocate memory \n"); 
      return -1; 
     } 
    } 

    /* Now, I'm setting some value to each 
     and every element in the 2d matrix */ 
    for (i=0; i<ROW ; i++) 
     for (j=0; j<COL ; j++) 
     arr[i][j].data = val++; 

    /* Now, I'm printing those values */ 
    for (i=0; i<ROW ; i++) 
     for (j=0; j<COL ; j++) 
     printf("arr[%d][%d].data = %d \n", i, j, arr[i][j].data); 

    for (i=0; i<ROW ; i++) 
     free(arr[i]); 
    free(arr); 

    return 0; 
} 

+0

在第一個示例中,您分配了一個矩陣。在你的第二個例子中,你分配了一個指向結構的指針。我想要兩個組合:) – andrei 2012-01-12 18:50:22

+0

@andrei我已經添加了一個新的程序。希望這就是你想要的! ..讓我知道,如果它可以幫助你!謝謝:) – 2012-01-12 19:06:20