【问题标题】:Create 2d array using pointers to a pointers in structure使用指向结构中的指针的指针创建二维数组
【发布时间】:2017-05-03 07:34:14
【问题描述】:

我刚开始使用结构,并有兴趣实现它以创建一个邻接矩阵以用于与图相关的算法实现。所以我在图中创建了一个指向指针变量的指针,将其用作二维矩阵的基地址。但是当我尝试为数组分配内存时,它向我显示了一个错误:

请求转换为非标量类型

谁能帮助我?我将整个代码发布在以下位置:-

struct graph{
    int v;
    int e;
    struct graph **admat;
};

void main()
{
    int x,i,y,z=1,n;
    struct graph *G=(struct graph **)malloc(sizeof(struct graph));
    printf("\nenter number of vertices: ");
    scanf("%d",&G->v);
    printf("\nenter number of edges: ");
    scanf("%d",&G->e);
    G->admat=(struct graph **)malloc(G->v * sizeof(struct graph *));
    for(i=0;i<G->v;i++)
    {
        G[i]=(struct graph)malloc(G->v * sizeof(int));//here is the main error
    }
    for(x=0;x<i;x++)
    {
        for(y=0;y<i;y++)
        {
            G[x][y]=z++;
        }
    }
    for(x=0;x<i;x++)
    {
        for(y=0;y<i;y++)
        {
            printf(" %d ",G[x][y]);
        }
        printf("\n");
    }
}

【问题讨论】:

  • malloc 中移除演员表。如果你想保持intstruct graph **admat; --> int **admat;
  • G 是一个单指针,但您正在转换双指针。那是不对的。我认为您对 G 应该是双指针还是结构元素 admat 是双指针感到困惑。这需要澄清。
  • 分配 G[x][y] = z 不正确。 G 是一个结构。它需要一个结构元素来存储 z 值。
  • 请注意,指向 T 的指针的指针 不是二维数组,也不是指向此类的指针。见stackoverflow.com/questions/42094465/…

标签: c pointers structure


【解决方案1】:

这段代码就是问题所在:

struct graph *G=(struct graph **)malloc(sizeof(struct graph));
printf("\nenter number of vertices: ");
scanf("%d",&G->v);
printf("\nenter number of edges: ");
scanf("%d",&G->e);
G->admat=(struct graph **)malloc(G->v * sizeof(struct graph *));
for(i=0;i<G->v;i++)
{
    G->admat[i]=(struct graph)malloc(G->v * sizeof(int));//here is the main error
}

你应该把它改成:

struct graph *G = malloc(sizeof(struct graph));
if (G == null)
    printf("Error allocating memory");

printf("\nenter number of vertices: ");
scanf("%d",&G->v);
printf("\nenter number of edges: ");
scanf("%d",&G->e);

G->admat=malloc(G->v * sizeof(struct graph *));  //  I guess you mean G->admat=malloc(sizeof(struct graph *));
if (G->admat == null)
    printf("Error allocating memory");
for(i = 0; i<G->v; i++)
{
    G[i] = malloc(G->v * sizeof(int));
    if (G[i] == null)
        printf("Error allocating memory");
}

应该被删除,因为您正试图为G 分配ints,这是一个指向struct graph 的双指针。这没有任何意义。

另请阅读this link,了解为什么不应该转换malloc 的结果。

【讨论】:

  • G[i]=(struct graph)malloc(G-&gt;v * sizeof(int)); --> G-&gt;admat[i]=malloc(G-&gt;v * sizeof(struct graph));,但 OP 可能想要持有 int
  • G[i] = malloc(G-&gt;v * sizeof(int)); 出现越界。
【解决方案2】:

假设 admat 包含二维矩阵数据, 这是代码。引入了一个新变量 z 来存储值 z。

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

struct graph{
    int v;
    int e;
    int z;
    struct graph **admat;
};

void main()
{
    int x,i,y,z=1,n;
    struct graph *G= malloc(sizeof(struct graph));
    printf("\nenter number of vertices: ");
    scanf("%d",&G->v);
    printf("\nenter number of edges: ");
    scanf("%d",&G->e);

    G->admat=malloc(G->v * sizeof(struct graph *));
    for(i=0;i<G->v;i++)
    {
        G->admat[i]=malloc(G->v * sizeof(struct graph));//here is the main error
    }
    for(x=0;x<i;x++)
    {
       for(y=0;y<i;y++)
       {
          G->admat[x][y].z=z++;
       }
    }
    for(x=0;x<i;x++)
    {
        for(y=0;y<i;y++)
        {
           printf(" %d ",G->admat[x][y].z);
        }
        printf("\n");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-04
    • 2017-06-11
    相关资源
    最近更新 更多