Getting info from an array of pointers

This is a discussion on Getting info from an array of pointers within the C Programming forums, part of the General Programming Boards category; Hi. I have an array of pointers (called g) that point to the dummy header of a linked list. The ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Unhappy Getting info from an array of pointers

    Hi.
    I have an array of pointers (called g) that point to the dummy header of a linked list.
    The linked list struct is as follows:

    Code:
    typedef struct node *node_ptr;
    struct node
    {
        int to_vertex;
        node_ptr next;
    };
    I have a separate int array, called e, which holds a number.

    What I am trying to do is to use a 'for loop' to travel along g, and at each element, look at the linked list it points to and decrement the number in the e array for each to_vertex in that linked list.
    eg, e[0] holds the number 2.
    if g[0] has a struct in its linked list with a to_vertex of 2, I want e[0] to become 1.
    I keep getting a segmentation fault. Can you please tell me where I am going wrong.

    Assuming i is the 'for loop' integer, and current has been declared as:
    Code:
    node_ptr current;
    this is the area the seg fault occurs:
    Code:
    current = g[i];
    while(current)
    {
    e[current->to_vertex]--;
    current=current->next;
    }
    It is the line e[current->to_vertex]--; that causes the problem.

    Thanks in anticipation of help
    Cathy

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    You need to show how you contructed your linked lists.

    Try using a debugger, and checking the value of current and current->to_vertex at the point of the crash.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    7

    Unhappy

    Thanks for the quick reply
    I used some printf statements to see what was in current and current->to_vertex, it seems that it points to the memory address rather than the actual value?

    Firstly, I constructed the linked list like this:
    Code:
    for (i=0; i<n_vertices; i++)
    {
    scanf("%d", &e[i]);		
    g[i] = create();		
    for(j=0; j<e[i]; j++)	
    {
        scanf("%d", &to); 
        insert_at_front(to, g[i]);	
    }
    where 'to' is a scanned int, and create() is:
    Code:
    node_ptr create(void)
    {
    	node_ptr list = (node_ptr) malloc(sizeof(struct node));
    	list->next = NULL;
    	return list;
    }
    and insert_at_front (with n equal to 'to' and list equal to g[i]) is:
    Code:
    void insert_at_front(int n, node_ptr list)
    {
    	node_ptr new_node = (node_ptr) malloc(sizeof(struct node));
    	new_node->to_vertex = n;
    	new_node->next = list->next;
    	list->next = new_node;
    }
    When I run it, I get the following output:

    The value of current is: 135536
    The value of current->to_vertex is: 137088
    Segmentation fault( core dumped)

    I really am stuck on this

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,334
    > g[i] = create();
    Your "dummy" node at the start of the list has no value stored in the to_vertex member.

    So you get whatever junk was in that memory when you created the first node.

    > node_ptr list = (node_ptr) malloc(sizeof(struct node));
    There's no need to cast malloc in C, see the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    7
    Thanks again
    I changed the create function to initialise a new node with a to_vertex of 0 and that has fixed the problem!.
    Thanks so much for pointing me in the right direction.
    Sometimes it is one simple small thing that causes a big problem...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning an Array of Pointers to Objects
    By randomalias in forum C++ Programming
    Replies: 4
    Last Post: 04-29-2006, 02:45 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. array of pointers to struct array
    By eth0 in forum C++ Programming
    Replies: 1
    Last Post: 01-08-2004, 05:43 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Replies: 5
    Last Post: 11-20-2001, 11:48 AM

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