[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 321
  • Last Modified:

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.
0
lewin85_sg
Asked:
lewin85_sg
4 Solutions
 
avizitCommented:
oen way would be to return a pair

make a pair out of the two pointers

http://www.sgi.com/tech/stl/pair.html
0
 
avizitCommented:
following example code i made should help you

#include<iostream>
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;
}
0
 
avizitCommented:
oops sorry , i thought i was in C technical area :(
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
sneeuw_chanCommented:
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.
0
 
Jaime OlivaresCommented:
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?
0
 
MysidiaCommented:
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;
}
0
 
Sys_ProgCommented:
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...

Amit
0
 
shajithchandranCommented:
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));
....
...


hold2->ptr1=-pointer1;
hold2->ptr2=-pointer2;
return hold2;
}

Don't forget to dellocate hold2 in the calling function once you take out the pointers.
0
 
Peter-DartonCommented:
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?

e.g.
/* 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;
  }
  else
    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.
e.g.
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;
  }
  else
    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)
0

Featured Post

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!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now