Like Tree1Likes
  • 1 Post By rags_to_riches

Help with malloc and structures?

This is a discussion on Help with malloc and structures? within the C Programming forums, part of the General Programming Boards category; I'm trying to use malloc to allocate memory for a couple of structures in a function. I've done my research ...

  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
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    What happens to your neighbors?
    Salem likes this.

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, 08:12 AM
  3. malloc in structures
    By kiros88 in forum C Programming
    Replies: 1
    Last Post: 08-25-2009, 01:53 PM
  4. pointers, structures, and malloc
    By lugnut in forum C Programming
    Replies: 24
    Last Post: 10-09-2008, 05: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


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21