How to pass a pointer inside a struct per function parameter?

Asked

Viewed 438 times

-1

The situation is as follows:

I have a struct with a field that is a pointer pointer, however I want to pass as parameter in a function only the pointer pointed, that is, the innermost pointer of that my field in struct:

In code it would be like this:

typdef struct
{  
  int **ponteiro;
}Ponteiro;

I have the address assignment this way:

 grafo->vertices[posicao_B].vertice_adjacente[(grafo->vertices[posicao_B].qtd_adj‌​acentes-1)] = &grafo->vertices[posicao_A];  <-- aqui estou tentando fazer com que o ponteiro de ponteiro (**vertice_adjacente) aponte para o endereço  de  (&grafo->vertices[posicao_A])

And I’m trying to print it like this, but nothing’s being printed:

printf("Vertice adjacente de A %d\n",(grafo->vertices[posicao_A].vertice_adjacente[0]->num_vertice));

Just remembering that (vertice_adjacent) is an array of pointers dynamically allocated by a function I developed.

If you can help me, I’d appreciate it!

  • Give more context on how you’re using this code.

  • In case, you’re wanting to pass int **ponteiro as a parameter or the first pointer of int **ponteiro, that would be int *ponteiro?

1 answer

0

typedef struct {int **ponteiro;} Ponteiro;

Ponteiro a;
int b;
int *c = &b;
a.ponteiro = &c;

fx(a.ponteiro);     // &c         : tipo int **
fx(*(a.ponteiro));  // &b         : tipo int *
fx(**(a.ponteiro)); // valor de b : tipo int
  • grafo->vertices[posicao_B]. vertice_adjacente[(grafo->vertices[posicao_B].qtd_adjacentes-1)] = &grafo->vertices[posicao_A];

  • So: I have a pointer of a graph struct, where internally there is a pointer of Vertices - this pointer was allocated as a vector of vertices - and in it there is a pointer of the type Vertices- being this field 'vertices_adjacent'.

  • Am I correctly assigning pointers? According to the code below: graph->vertices[posicao_A]. vertice_adjacent[(graph->vertices[posicao_A].qtd_adjacent)] = &graph->vertices[posicao_B]; And your print: printf("Adjacent vertices of A %d n",(graph->vertices[posicao_A].vertice_adjacent[0]->num_vertices)); Am I doing it correctly? Thanks in advance.

  • Change your question with this new information. Here in a comment it is not easy to understand the code correctly.

  • I edited the question, I think you can understand now.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.