Thread: Help with malloc and structures?

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    1

    Help with malloc and structures?

    I'm trying to use malloc to allocate memory for a couple of structures in a function. I've done my research but haven't been able to find anything that's helped me. Here are the structures:

    Code:
        typedef struct _node
        {
        char letter;
        int num_neighbors;
        struct _node *neighbors[8];
        } node;
    
        typedef struct _graph
        {
        int num_nodes;
        struct _node *nodes; /* pointer to first element in an array of   nodes */
        } graph;
    Here's what I've been trying:
    Code:
        graph* graph_create(int size)
        {
            node** neighbors = malloc(8*sizeof(node*));
            for (int count = 0; count < 8; count++)
            {
                neighbors[count] = malloc(sizeof(node));
            }
        graph *newGraph;
        newGraph = malloc(sizeof(graph));
        newGraph->num_nodes = size;
        
    
        return newGraph;
    
    
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    What happens to your neighbors?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with pointers, structures, and malloc
    By tmd2 in forum C Programming
    Replies: 11
    Last Post: 03-04-2011, 08:24 AM
  2. malloc() with array of structures
    By khpuce in forum C Programming
    Replies: 8
    Last Post: 10-26-2010, 07:12 AM
  3. malloc in structures
    By kiros88 in forum C Programming
    Replies: 1
    Last Post: 08-25-2009, 12:53 PM
  4. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 04:52 PM
  5. malloc ing array's or structures
    By Markallen85 in forum C Programming
    Replies: 4
    Last Post: 02-03-2003, 01:23 PM

Tags for this Thread