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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 :(
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses

777 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