How to return pointers array?

Posted on 2004-09-19
Medium Priority
Last Modified: 2010-04-15
Currently I have a function that updates a linkedlist. The function prototype is as such:
struct dbLinkedlist* insertError(struct dbLinkedlist* startPtr, struct dbLinkedlist* list);

As you can see, the function now returns one pointer. I need the function to return two pointers, how should I do it?

Pls help. thx.
Question by:lewin85_sg
LVL 11

Expert Comment

ID: 12094693
oen way would be to return a pair

make a pair out of the two pointers

LVL 11

Expert Comment

ID: 12094713
following example code i made should help you

using namespace std;

pair<int *, int *> test(){
   int *a = new int(2);
   int *b = new int(3);
   return(make_pair(a, b));

int main(){

  pair<int *, int *> result = test();

  cout<<"first: "<<*(result.first)<<endl;
  cout<<"second: "<<*(result.second)<<endl;
LVL 11

Expert Comment

ID: 12094715
oops sorry , i thought i was in C technical area :(
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.


Assisted Solution

sneeuw_chan earned 300 total points
ID: 12094796
There are (at least) two ways:

You could make the arguments to the function pointers-to-pointers, and update those:

> void insertError(struct dbLinkedlist** startPtr, struct dbLinkedlist** list);

Or you could use the 'pair' approach, but do it the good old C way, with a struct:

> struct two_dbLinkedlists { struct dbLinkedlist *a, *b };
> struct two_dbLinkedlists insertError(struct dbLinkedlist* startPtr, struct dbLinkedlist* list) { ... }

The first method is how almost all C programmers do it, I'm not even sure if the second method works as written.
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12096216
Are you sure you need to return two pointers? I have implemented linked lists many times but didn't need that, could you give us some detail?
LVL 23

Accepted Solution

Mysidia earned 600 total points
ID: 12097663
Use a pointer to an array with those two pointers in it if they are of the same type.

dbLinkedlist** insertError(struct dbLinkedlist* startPtr, struct dbLinkedlist* list);

so  result[0] == the first pointer,  result[1] == the second pointer, etc

If they're of different type, either use void* or  return a structure.

void* insertError(struct dbLinkedlist* startPtr, struct dbLinkedlist* list);

inside insertError
   array = malloc(sizeof(void *) * 2);
   array[0] = (void *) pointer1;
   array[1] = (void *) pointer2;

be sure parent frees the array when done.  (casting the pointers back to the correct type)

In the new versions of the C language it's allowed for functions to return structures... this is probably
the best way to accomplish it in a modern environment (if available):

typedef TWO_POINTERS(struct dbLinkedlist, long)  linklistLong;

typedef struct{ struct dbLinkedlist element;   long other; ... }  myFuncResult;

myFuncResult myFunction() {
          myFuncResult  result;

          result.element = whatever;
          result.other = whatever2;

         return result;
LVL 10

Expert Comment

ID: 12099149
I agree with jaime_olivares,

the need to return two pointers seems dubious........Although possible to do it, but just think if u really need it...


Assisted Solution

shajithchandran earned 300 total points
ID: 12143239
I think the simplest way to do is to define a struct that can hold two pointers and return that.

struct tag
void * ptr1;
void *ptr2;

struct tag* fun(....)
struct tag *hold2=(struct tag *)malloc(sizeof(struct tag));

return hold2;

Don't forget to dellocate hold2 in the calling function once you take out the pointers.

Assisted Solution

Peter-Darton earned 300 total points
ID: 12522685
Personally, I'd be rather against allocating memory just to hold some data that's going to be freed up immediately.

If you want to return two items of data, why not demand that the caller provide you with two places to store the results?

/* returns 0 if successful, returns data in *first_string and *second_string */
int returns_two_strings(char **first_string, char **second_string)
  if( first_string && second_string )
    *first_string = "hello";
    *second_string = "world";
    return 0;
    return 1;

  char *res_a;
  char *res_b;
  if( returns_two_strings(&res_a,&res_b)==0 )
    printf("Got two pointers to strings now: %s %s\n", res_a, res_b);

Allocating memory is fine for long-term storage, but it's inefficient for short term stuff.

Note: Some compilers allow you to return a structure (i.e. the structure contents, not merely a pointer to a structure) - if yours does, then that's probably better.

I'd suggest that if you want to return more than 2 things, define a structure to contain the results and then ask the caller to provide a pointer to such a structure, and put your results in there.
struct myresults
  char *first_string;
  char *second_string;
/* returns 0 if successful, returns data in *results */
int returns_two_strings(struct myresults *results)
  if( results )
    results->first_string = "hello";
    results->second_string = "world";
    return 0;
    return 1;

  struct myresults res;
  if( returns_two_strings(&res)==0 )
    printf( "Got two pointers to strings now: %s %s\n", res.first_string, res->second_string );

Finally, however, I'd agree with the other folks who questioned why you need to do this in the first place.
If you're writing a method that inserts something into a doublylinked list, then the first parameter shouldn't be a pointer to the head element, it should be the location of where the pointer to the head element is stored.
i.e. don't do
  void insertAnElement( struct myElement *head, struct myElement *newEntryToBeAdded)
  insertAnElement( listHead, newEntry );
instead you want to do
  void insertAnElement( struct myElement **headPtr, struct myElement *newEntryToBeAdded)
  insertAnElement( &listHead, newEntry );
this way the insertAnElement method can modify the "listHead" variable to point to something else if it needs to.  Obviously, if you're storing both ends of the list, the same applies - pass in a pointer to the variables that store the head and the tail.

(Note: In C++ you'd pass these in as a reference, but in C we don't have those, so we have to just use a pointer and then make sure that pointer isn't NULL)

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

587 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question