• C

How to return pointers array?

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.
Who is Participating?
MysidiaConnect With a Mentor Commented:
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;
oen way would be to return a pair

make a pair out of the two pointers

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;
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

oops sorry , i thought i was in C technical area :(
sneeuw_chanConnect With a Mentor Commented:
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.
Jaime OlivaresSoftware ArchitectCommented:
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?
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...

shajithchandranConnect With a Mentor Commented:
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.
Peter-DartonConnect With a Mentor Commented:
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)
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.

All Courses

From novice to tech pro — start learning today.