Thread: how to return address of a pointer?

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    how to return address of a pointer?

    Hi there!
    I'm tackling linked lists for the first time, a topic that we haven't yet covered in my basic C course but is illustrated quite nicely in Standish's Data Structures, Algorithms & Software Principles In C.
    I need some help with a function in my linked list test program. Basically it just returns the address of the nth node on list L:
    Code:
    car *getdrs(car *L, int nodeNum) {    // (car is the structure type for my parking garage program)
    	if (L == NULL || nodeNum > getLength(L))
    		return NULL;
    	else {
    		car *N = L;
    		int i = 1;
    // if nodeNum is 0, return address of the last node in L
    		while (N->link != NULL && (i <= nodeNum || nodeNum == 0)) {
    			N = N->link;
    			++i;
    		}
    		return N;
    	}
    }
    since this is my first stab at really using pointers I'm not sure if everything is OK, but it seems to work alright. This is what I'm having trouble with: say you want to directly access a field of the nth node with getadrs, as in:
    getdrs(List, 2)->link
    this works fine as long as there is a 2nd node in the list. If the list is empty, or if there are less than 2 nodes in List, the function returns NULL, and the program obviously crashes.
    What I'd rather have the program do in this situation is return the address of List itself- would this be at all useful? I realize you could prevent this kind of error by checking the validity of the function call with a preceding if statement, but I want to try and make the getdrs function immune to pointer access violations or w/e by returning the argument that was passed to it in the first place.

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Quote Originally Posted by spotty View Post
    this works fine as long as there is a 2nd node in the list. If the list is empty, or if there are less than 2 nodes in List, the function returns NULL, and the program obviously crashes.
    What I'd rather have the program do in this situation is return the address of List itself- would this be at all useful? I realize you could prevent this kind of error by checking the validity of the function call with a preceding if statement, but I want to try and make the getdrs function immune to pointer access violations or w/e by returning the argument that was passed to it in the first place.
    Not really, it would be more useful (IMO) if you returned the node closest to "2". The only problem then, what if there are no elements in the list? You can't really make it "NULL safe" so to speak.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  3. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  4. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  5. problem with open gl engine.
    By gell10 in forum Game Programming
    Replies: 1
    Last Post: 08-21-2003, 04:10 AM