returning pointers

This is a discussion on returning pointers within the C++ Programming forums, part of the General Programming Boards category; Hi I've spent a long time on the web & reading books trying to find out how to do this ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    returning pointers

    Hi

    I've spent a long time on the web & reading books trying to find out how to do this for myself.
    No luck, there's a lot of stuff refering to using pointer to a function but very little on returning a pointer and it doesn't appear to do what I want it to
    .
    So I'm hopping someone knows how to return a pointer

    I want to call a procedure and for that procedure to return a pointer.
    The procedure builds a linked list of a struct

    Code:
    struct1 read()
    {			
    
    	// init stuff
    	struct1 *start_pointer;
    
    	loop till end
    	get data
    	create new record for list
    	insert into list
    	if this is the first recored, set a pointer to the start of the list
    
    	//
    
    	return start_pointer
    }
    
    main
    {
    	//init stuff
    	struct1 *ptr;
    	ptr = read();  //I want ptr to point to the start of the linked list created in read
    
    	// other stuff
    }

    It looks like I'm passing pointers to pointers, but I get an error -
    <cannot convert from 'struct netData *' to 'const struct netData'>

    Prefixing the return parameter with a * gives error on the calling line
    <binary '=' : no operator defined which takes a right-hand operand of type 'struct netData'>

    while prefixing with & gives -
    <cannot convert from 'struct netData ** ' to 'const struct netData'>

    I also get errors prefixing both ptr and the calling read
    - yes I am clutching at straws.

    I have tried using this, but also get errors here

    Gratefull for any help given

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Code:
    int *read(void);
    
    int *read(void)
    {
    	int *a = new int;
    	*a = 0;
    	return a;
    }
    
    int main()
    {
    	int *x = read();
    	delete x;
    	
    	return 0;
    }
    Works fine for me.

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    For a start you are missing a semicolon:
    return start_pointer
    A simple function to return a pionter would be something like:
    Code:
    struct1 *GetPointer()          
    { 
        return pointer; 
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,848
    struct1 *read()
    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
    Nov 2006
    Posts
    519
    Quote Originally Posted by spudval View Post
    So I'm hopping someone knows how to return a pointer
    Don't.
    your function is a source function. that means it allocates memory and returns a handle to it to some sink function. returning a pointer possibly results in a memory leak. in projects more complex than a few lines of code the caller will forget to call delete some day. c++ has auto_ptr for source and sink functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Double pointers and linked lists.
    By simo_r in forum C Programming
    Replies: 2
    Last Post: 05-06-2008, 05:25 AM
  2. need help with extern pointers.
    By broli86 in forum C Programming
    Replies: 17
    Last Post: 04-11-2008, 10:16 AM
  3. API "Clean Up" Functions & delete Pointers :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-10-2002, 07:53 PM
  4. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM

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