A question about pointers in functions

This is a discussion on A question about pointers in functions within the C Programming forums, part of the General Programming Boards category; I'm programming (in Ansi C) an adjacency list of an array with each element pointing to a list of structs ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    17

    A question about pointers in functions

    I'm programming (in Ansi C) an adjacency list of an array with each element pointing to a list of structs and compiling with

    gcc -Wall -ansi -pedantic -O *.c

    my question is simply that from within main I can easily move down and print out all of these lists BUT...

    if I try to do this with a function I can't seem to get it to print more than the first list at array[o].

    for example in main this prints all lists

    Code:
    k = 0;
    	
    	while(k < n_vertices)
    	{
    	    printf("\n\n");
    		printf("List: %d\n", k);
    		print(*g++);
    		k++;
    	}
    while in my file nodes.c in the same directory this call from main

    Code:
    print_all_lists(*g, n_vertices); /* n_vertices is the sizeof the array; */
    means I can't seem to get the iterative call to print working...


    Code:
    void print_all_lists(node_ptr arr, int arr_length)
    {
    	int k = 0;
    	
    	while(k < arr_length)
    	{
    	    printf("\n\n");
    		printf("List: %d\n", k);
    		
    		print(arr);
    
    		k++;
    	}
    }
    Obviously print(arr) in nodes.c is going to repeatedly print the first list but every way I've tried to increment down (like I did in main) just makes the program crash.

    Could anyone offer any advice into what I am missing in my understanding. Why could I do this in main but can't seem to do so from other files?

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Why not just pass a pointer to the first node in print_all_lists?

    such as
    Code:
    node_ptr * g;
    
    print_all_lists(g, n_vertices); /* n_vertices is the sizeof the array; */
    
    void print_all_lists(node_ptr * arr, int arr_length)
    {
         /* ... */
    }
    I would suggest a linked list.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    17
    Hi Zacs7, yes it is already a pointer to a linked list...

    what I'm saying is from main() the code using 'g' will print out the entire set of lists iterating down the array of pointers...

    but when I try from a function on another file I can't get it to print out all of the lists in the same way by passing a 'print(*arr++)' call for example.

    I'll hunt up the reason today but have just become very curious as to why exaclty I could pass that *g++ from main but can't achieve the same result from a function on the same file as print(node_ptr the_list).

    Thanks for the reply though, feedback is appreciated.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Sounds like a typical "defined a struct in a header file, but didn't recompile all .c files that use the definition" problem to me.

    e.g. If you change a header file, you should recompile all files that include that header.

    Just a hunch, but that's what it looks like...
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Some help with class member functions and pointers
    By k_rock923 in forum C++ Programming
    Replies: 1
    Last Post: 07-21-2005, 01:28 AM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Pointers, arrays , functions
    By sballew in forum C Programming
    Replies: 19
    Last Post: 09-17-2001, 12:12 AM

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