一个关于C中的对象的指针矩阵

时间:2022-09-06 15:19:09

I assume this is basic question, but I'm not English and not used still with the terms of programming language, that's why I question here (I couldn't find).

我认为这是一个基本的问题,但我不是英语,也没有使用编程语言的术语,这就是我在这里提出问题的原因(我找不到)。

Here's my context:

这是我的背景:

I have a structure (let's simplify it) as follow

我有一个结构(我们化简一下)如下

struct _unit
{
  char value;
} Unit;

and in the main program, I'd like to have a row of pointers that points a row of other pointers pointing structures Unit. Something like

在主程序中,我希望有一行指针指向另一行指针指向结构单元。类似的

int main ()
{
  Unit** units;

  ..

  printf("%d", units[0][0].value);

  ...
}

I get a little confused, what is the way to go so Unit's can be accessed as a multi-dimensional array.

我有点搞不懂,怎么做才能让单位作为多维数组访问。

here's my try

这是我的尝试

{
  units = (Unit**)malloc(sizeof(void*));

  units[0][0] = (Unit*)malloc(sizeof(Unit));
}

2 个解决方案

#1


4  

You're almost there.

你差不多了。

First, you need to declare your type, as you have, as Unit**. Then, on that, allocate enough Unit* pointers to hold the number of rows. Finally, loop over those created pointers, creating each Unit.

首先,需要将类型声明为单元**。然后,在此基础上,分配足够的单元*指针来保存行数。最后,循环这些创建的指针,创建每个单元。

For example:

例如:

int i;
Unit** u;

// Allocate memory to store 50 pointers to your columns.    
u = malloc(sizeof(Unit*) * 50);

// Now, for each column, allocate 50 Units, one for each row.
for(i=0; i<50; i++) {
    u[i] = malloc(sizeof(Unit) * 50);
}

// You can now access using u[x][y];

That's the traditional way of doing it. C99 also introduced a different syntax to do this, as follows:

这是传统的做法。C99还引入了不同的语法,如下所示:

Unit (*u)[n] = malloc(sizeof(Unit[n][n])) // n = size of your matrix

From: https://*.com/a/12805980/235700

来自:https://*.com/a/12805980/235700

#2


2  

You may have misunderstood the meaning of the struct declaration.

您可能误解了struct声明的含义。

struct _unit
{
    char value;
} Unit;

The effect of your code is to declare a struct type struct _unit and allocate space for one variable of that type, named Unit.

代码的作用是声明一个struct类型struct _unit,并为该类型的一个变量(命名为Unit)分配空间。

What you probably meant to say is

你可能想说的是

struct _unit
{
    char value;
};

typedef struct _unit Unit;

In C, the word "struct" is part of the type unless you typedef it.

在C语言中,“struct”是类型的一部分,除非你键入它。

#1


4  

You're almost there.

你差不多了。

First, you need to declare your type, as you have, as Unit**. Then, on that, allocate enough Unit* pointers to hold the number of rows. Finally, loop over those created pointers, creating each Unit.

首先,需要将类型声明为单元**。然后,在此基础上,分配足够的单元*指针来保存行数。最后,循环这些创建的指针,创建每个单元。

For example:

例如:

int i;
Unit** u;

// Allocate memory to store 50 pointers to your columns.    
u = malloc(sizeof(Unit*) * 50);

// Now, for each column, allocate 50 Units, one for each row.
for(i=0; i<50; i++) {
    u[i] = malloc(sizeof(Unit) * 50);
}

// You can now access using u[x][y];

That's the traditional way of doing it. C99 also introduced a different syntax to do this, as follows:

这是传统的做法。C99还引入了不同的语法,如下所示:

Unit (*u)[n] = malloc(sizeof(Unit[n][n])) // n = size of your matrix

From: https://*.com/a/12805980/235700

来自:https://*.com/a/12805980/235700

#2


2  

You may have misunderstood the meaning of the struct declaration.

您可能误解了struct声明的含义。

struct _unit
{
    char value;
} Unit;

The effect of your code is to declare a struct type struct _unit and allocate space for one variable of that type, named Unit.

代码的作用是声明一个struct类型struct _unit,并为该类型的一个变量(命名为Unit)分配空间。

What you probably meant to say is

你可能想说的是

struct _unit
{
    char value;
};

typedef struct _unit Unit;

In C, the word "struct" is part of the type unless you typedef it.

在C语言中,“struct”是类型的一部分,除非你键入它。