double pointer to struct inside struct


How can i access to a duble pointer in a struct pointer?? with the code bellow, calling addBow() give me a Segmentation fault (core dumped) error

typedef struct { int size; tCity **cities; }tGraph; //para iniciar el grafo void initGraph(tGraph *graph, int size) { graph = (tGraph*)malloc(sizeof(tGraph)); graph->cities = (tCity**)malloc(sizeof(tCity*) * size); graph->size = size; } //agrega un arco entre ciudades void addBow(tGraph *graph, int id, tCity *city) { if ( graph->cities[id] == NULL ) { graph->cities[id] = city; } else { tCity *cur = graph->cities[id]; while ( getNext(cur) != NULL ) { cur = getNext(cur); } setNext(cur, city); } }

which is the correct syntax for graph->cities[id]??


SOLUTION: editing the initGraph solve the problem since the memory wasn't allocated

tGraph* initGraph(int size) { tGraph *graph = (tGraph*)malloc(sizeof(tGraph)); graph->cities = (tCity**)malloc(sizeof(tCity*) * size); graph->size = size; return graph; }


You should either have initGraph() take (**graph) or return the graph. Since the malloc address of graph is local to initGraph.

Something like:

void initGraph(tGraph **graph, int size) { tgraph *temp; temp = (tGraph*)malloc(sizeof(tGraph*)); temp->cities = (tCity**)malloc(sizeof(tCity*) * size); temp->size = size; *graph = temp; }


graph = (tGraph*)malloc(sizeof(tGraph*));

There is one of your problems... it should be graph = malloc(sizeof(tGraph));


Make initGraph () return a pointer to tGraph.

tGraph* initGraph(int size) { tGraph* graph; graph = malloc(sizeof(tGraph)); graph->cities = malloc(sizeof(tCity*) * size); graph->size = size; return graph; }


  • addEventListener() in Lua
  • Sprox form with Turbogears, using Mako, only displays plain text
  • Linux: Is it possible to sandbox shared library code
  • MPI_Gather() the central elements into a global matrix
  • Memory allocation for incremental garbage collection simulation in c++
  • linked list program
  • Cannot connect to my database from within SQL Management Studio
  • Where does git store the commit message
  • MPI_Gather segmentation fault
  • Deleting every odd positioned node in a linked list in C
  • How to process a JPEG binary data in OpenCV?
  • a nested struct with pointers
  • Confused about accessing struct members via a pointer
  • Difference between linked list traversal **while(thead != NULL)** and while(thead->next !=NULL)
  • Rails 4: Removing grid lines chartkick?
  • Get Distinct Parent Items using Lambda
  • Static const int not good enough for array size?
  • Difference between 2D char Array and char** (OR, 3D char Array and char*** etc)
  • iOS/Objective C: Converting RGB Data to UIImage
  • Conflicting Types Error
  • Can we reuse allocated memory
  • Salted sha512 in C, cannot synchronise with Symfony2's FOSUserBundle
  • Google web service to get time by location? [duplicate]
  • Getting segmentation fault while using malloc
  • JPA - getting distinct value from one column
  • Rely on Facebook user id as a permanent user identifier
  • Randomly placing a polygon inside of polygon
  • Ransack search string arrays stored in db
  • Django: ORDER BY DESC on FloatField puts null values top?
  • How to add learning rate to summaries?
  • Double dispatch in Java example
  • why overloaded new operator is calling constructor even I am using malloc inside overloading functio
  • Change multiple background-images with jQuery
  • Android screen density dpi vs ppi
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Change an a tag attribute in JavaScript based on screen width
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Sorting a 2D array using the second column C++
  • java string with new operator and a literal