1
I’m trying to make an array of dynamic structs in C, but I don’t understand why it’s returning to me.
The code is basically this:
I’m just creating a vector of 10 arrays and then trying to initialize each struct individually, someone can help me?
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#define MAX_NODES 10000
#define INF 999999999
typedef struct Node{
int value;
int listaAdj[MAX_NODES];
int pesoArestas[MAX_NODES];
int visited;
}Node;
//variaveis globais
Node *grafo;
int numVertices = 10;
int main(){
int i,j;
grafo = (Node*) malloc((numVertices) * sizeof(struct Node*));
for ( i = 0; i < numVertices; i++ ){
if ( i == 0 ){
grafo[i].value = 0;
}else{
grafo[i].value = INF;
}
for ( j = 0; j < MAX_NODES; j++ ){
grafo[i].listaAdj[j] = -1;
grafo[i].pesoArestas[j] = -1;
}
grafo[i].visited = 0;
}
for ( i = 0; i < numVertices; i++ ){
printf(" %d \n", grafo[i].value);
}
free(grafo);
return 0;
}
Sorry guys, I can’t code my question, how do I do that?
– avoid
Pasting it and using the appropriate marker in the editor. This is what appears
{ }
– Maniero
And now I’m on show, they can spot the problem?
– avoid
Ps: forgot to put, MAX_NODES is set to 10,000
– avoid
@avoid because you took a relevant part of the code? This way it became difficult to solve something.
– Maniero
my entire code is there, only missing imports of stdlib and stdio
– avoid
It wasn’t, you had it off and I put it back.
– Maniero
@avoid, Whenever it is a question of the type "why my code doesn’t work?", remember to include a code complete and compilable on the question. Without this others have neither how to test.
– Guilherme Bernal
See more on http://answall.com/help/mcve
– Maniero