-2

I just started structures and was interested to implement it to create an adjacency matrix to use in graph related algorithm implementation. So I created a pointer to pointer variable in the graph to use it as the base address for the 2D matrix. But when I tried to assign memory to the array it is showing me an error:

Conversion to non-scalar type requested

Can anyone help me? I am posting the whole code in the following:-

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");
    }
}
Toby
  • 9,696
  • 16
  • 68
  • 132
Debian95
  • 1
  • 3
  • 1
    Remove cast from `malloc`. and If You want hold `int`, `struct graph **admat;` --> `int **admat;` – BLUEPIXY May 03 '17 at 07:38
  • G is a single pointer but you are typecasting double pointer. That is not right. I think you are confused whether G supposed to be a double pointer or the struct element admat is a double pointer. That needs to be clarified. – Nguai al May 03 '17 at 08:01
  • Assigning G[x][y] = z is not correct. G is a structure. It needs a structure element to store z value. – Nguai al May 03 '17 at 08:02
  • Note that a *pointer to pointer to T* is not a 2d array, or a pointer to such. See http://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays – Ilja Everilä May 03 '17 at 08:17

2 Answers2

1

This piece of code is the problem :

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
}

You should change it to :

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

should be removed, as you are trying to allocate ints for G, which is a double pointer to struct graph. It does not make any sense.

Also read this link on why you should not cast the result of malloc.

Community
  • 1
  • 1
Marievi
  • 4,951
  • 1
  • 16
  • 33
0

Assuming that admat contains 2D matrix data, here is the code. A new variable z is introduced to store the value 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");
    }
}
Nguai al
  • 958
  • 5
  • 15