How do I access the pointer of a struct within the pointer of another struct?

1
typedef struct vertice
{  
    int num_vertice;    
    int profundidade;    
    struct vertice **vertices_adja;

} Vertice;

typedef struct grafo
{    
   int qtd_vertices;   
   int *Vertice;

} Grafo;

I want to access the attributes of the 'Vertice' struct within the graph, the way I'm trying to do is this.

Grafo* criarGrafo(int *vertices, int qtd_vertices)
{     
   Grafo *grafo_A;

   grafo_A = malloc(sizeof(Grafo));

   grafo_A->Vertice = alocarVertices(qtd_vertices);

    if(grafo_A->Vertice != NULL)
    {
       grafo_A->qtd_vertices = qtd_vertices;

       int i = 0 ;

       for( ; i < qtd_vertices; i++)
       {
         (grafo_A)->Vertice[i]->num_vertice = vertices[i];  <-- Aqui está erro "error: invalid type argument of ‘->’ (have ‘int’)"
       }
        return grafo_A;
     }
}
    
asked by anonymous 06.10.2015 / 16:07

2 answers

1

Nete local where you point as error has a small concept failure

(grafo_A)->Vertice[i]->num_vertice = vertices[i];

(grafo_A) is a logo pointer - > is correct... You access Vertice because you used indexing, that is, Vertice [i] is an instance not a pointer should soon have been used. as below

(grafo_A)->Vertice[i].num_vertice = vertices[i];

This is because every vector / array is a pointer ...

int a[10];
cout<<*(a)<<endl; // 'a' aponta para o primeiro endereço a[0] de memoria

// a+9 é o ultimo endereço do vetor e *(a+9) é a instancia do endereço
// então, *(a+9) == a[9]
cout << a[9] << "==" << *(a+9);

That is, another possible solution would be to write as follows:

(grafo_A)->(Vertice + i)->num_vertice = vertices[i];
    
06.10.2015 / 16:30
1

Vertice within struct grafo has type int * . These two types are not supported.
When you index a value of type int * obtens a int and a value of type int has no members.

Defines struct grafo as containing a pointer to struct vertice

typedef struct vertice
{
    int num_vertice;
    int profundidade;
    struct vertice **vertices_adja;
};

typedef struct grafo
{
   int qtd_vertices;
   struct vertice *Vertice;
};

Deposits of this you can do

    grafo_A->Vertice[i].num_vertice = vertices[i];
    
06.10.2015 / 16:17