English 中文(简体)
C中数据类型混合的点数矩阵
原标题:Matrix of pointers with mixed data types in C
  • 时间:2012-01-12 18:32:15
  •  标签:
  • c

I am trying to define a matrix like so I have a structure

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));

问题在于,我无法获得我的Matrix[last_index].它所说的数据,也尝试了——和(实际上不知道何时使用什么)

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

什么是错的? 是否应当将实际守则张贴在后面? 如果不可能采用这种方法,那就能够提出不同的建议?

最新资料:我再说一遍,汇总表都暗中,只是要最后一点指出这一结构,一些评论没有考虑到这一点。 这是我以身作则的方式宣布的。

最佳回答

让我们开始简单:

您希望创建<代码>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]);
  }
}

如欲查阅ith 和j th elements,请注明:

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

<><>Edit>/strong>

Ah, now I get it.

不要这样做。

没有保证点码 类型与点码<>int相同。 它们是建立在大多数共同结构之上的,但这不是你可以依赖的东西。 如果他们不敢,那么你会再遇到麻烦。

从设计的角度来看,这只是使我感到迷惑;如果你需要将这种结构与矩阵联系起来,就会产生一种新的总合,以明确做到:

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

如果你需要释放汇总表,这将使生活更加容易。

FWIW,为了做你想要做的事情,你需要参加一些体育锻炼,例如:

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

and

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

但是,正如我前面所说,如果点数类型不兼容,转换可能会造成时间错误。 页: 1 Bad juju。

问题回答

您应将点名改为适当类型:

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

否则,你试图在<条码>中找到一个名为<条码>的数据的外地,当然不能这样做。 或者,将.

下面是你应做些什么,以动态方式使用<条码>(<>/代码>)。

#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 
");
        return -1;
    }

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

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

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

    return 0;
}

OTOH, to access the following struct member variables you have the following ways

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

Option #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 
")
    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);

希望!


Based on the request from the OP author, combining the above two programs into one.

#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 
");
        return -1;
    }

    for (i=0; i<ROW ; i++) {
        if ((arr[i] = malloc(sizeof(myDataType) * COL)) == NULL) {
            printf("unable to allocate memory 
");
            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 
", i, j, arr[i][j].data);

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

    return 0;
}





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签