【问题标题】:Accessing pointers within a struct访问结构中的指针
【发布时间】:2012-05-05 20:16:43
【问题描述】:

目前我有以下代码:

typedef struct _hexagon {
    int *vertice[6];
    int *path[6];
    int resourceType;
} hexagon;


typedef struct _game {
    hexagon hexagons[5][5];
} Game;

我主要有:

Game g;
// This is the line that fails
g.hexagons[0][0].vertice[0] = 0;

这编译得很好,但会出现分段错误。我尝试了很多变体,例如

g.hexagons[0][0].*vertice[0] = 0;

无法编译。如何从结构中访问指针的内存?

【问题讨论】:

  • 为什么顶点是int*数组而不是int数组?
  • 另外,你说的段错误代码在 Mac OS X 上不适合我。你在什么系统上运行?
  • @DougRichardson 你在运行 C 程序还是 C++ 程序?
  • g.hexagons[0][0].vertice[0] = 0; 行失败怎么说?您是否正在查看gdb 上的堆栈跟踪?在这种情况下,您应该使用 gdb - 对吗?!

标签: c arrays pointers segmentation-fault


【解决方案1】:

由于verticearray-of-pointes-to-integers,要访问vertice[0],你需要做*g.hexagons[0][0].vertice[0]

示例程序:

#include <stdio.h>

typedef struct _hexagon {
    int *vertice[6];
    int *path[6];
    int resourceType;
} hexagon;


typedef struct _game {
    hexagon hexagons[5][5];
} Game;

int main()
{
    int i1 = 1;
    int i2 = 2;
    int i3 = 3;
    int i4 = 4;
    int i5 = 5;
    int i6 = 6;

    Game g;
    g.hexagons[0][0].vertice[0] = &i1;
    g.hexagons[0][0].vertice[1] = &i2;
    g.hexagons[0][0].vertice[2] = &i3;
    g.hexagons[0][0].vertice[3] = &i4;
    g.hexagons[0][0].vertice[4] = &i5;
    g.hexagons[0][0].vertice[5] = &i6;

    printf("%d \n", *g.hexagons[0][0].vertice[0]);
    printf("%d \n", *g.hexagons[0][0].vertice[1]);
    printf("%d \n", *g.hexagons[0][0].vertice[2]);
    printf("%d \n", *g.hexagons[0][0].vertice[3]);
    printf("%d \n", *g.hexagons[0][0].vertice[4]);
    printf("%d \n", *g.hexagons[0][0].vertice[5]);

    return 0;   
}

输出:

$ gcc -Wall -ggdb test.c 
$ ./a.out 
1 
2 
3 
4 
5 
6 
$ 

希望对你有帮助!


更新:正如 Luchian Grigore 指出的那样

分段错误的原因由下面的小程序解释。简而言之,您正在取消引用 NULL 指针。

#include <stdio.h>

/*
int *ip[3];
+----+----+----+
|    |    |    |
+----+----+----+
   |    |    |
   |    |    +----- points to an int *
   |    +---------- points to an int *
   +--------------- points to an int *

ip[0] = 0;
ip[1] = 0;
ip[2] = 0;

+----+----+----+
|    |    |    |
+----+----+----+
   |    |    |
   |    |    +----- NULL
   |    +---------- NULL
   +--------------- NULL

*ip[0] -> dereferencing a NULL pointer ---> segmantation fault
*/

int main()
{
    int * ip[3];
    ip[0] = 0;
    ip[1] = 0;
    ip[2] = 0;

    if (ip[0] == NULL) {
        printf("ip[0] is NULL \n");
    }

    printf("%d \n", *ip[0]);
    return 0;
}

现在您可以将int *ip[] 与您的g.hexagons[0][0].vertice[0] 关联起来

【讨论】:

  • 为什么g.hexagons[0][0].vertice[0] = 0; 会失败?
  • 你也错过了一些括号。你写*g.hexagons[0][0].vertice[0],我相信你的意思是(*g.hexagons[0][0].vertice)[0]
  • 他没有那个,他的代码在g.hexagons[0][0].vertice[0] = 0;崩溃,在你的例子中实际上是ip[0] = 0;
  • 感谢您的帮助,您添加的解释特别有见地。
【解决方案2】:

我认为您可能误解了您在_hexagon 中声明的内容。 *vertice[6] 和您的其他数组成员都是指针数组,因此您必须将每个元素视为指针。

int x = 10;
g.hexagons[0][0].vertice[0] = &x;

x 的地址存储到指针数组位置 0 的指针中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 1970-01-01
    • 2017-01-16
    • 1970-01-01
    • 2021-12-11
    • 1970-01-01
    相关资源
    最近更新 更多