Solved

How to return pointers array?

Posted on 2004-09-19
10
289 Views
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.
0
Comment
Question by:lewin85_sg
10 Comments
 
LVL 11

Expert Comment

by:avizit
ID: 12094693
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
 
LVL 11

Expert Comment

by:avizit
ID: 12094713
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
 
LVL 11

Expert Comment

by:avizit
ID: 12094715
oops sorry , i thought i was in C technical area :(
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 2

Assisted Solution

by:sneeuw_chan
sneeuw_chan earned 100 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.
0
 
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?
0
 
LVL 23

Accepted Solution

by:
Mysidia earned 200 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;
}
0
 
LVL 10

Expert Comment

by:Sys_Prog
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...

Amit
0
 
LVL 5

Assisted Solution

by:shajithchandran
shajithchandran earned 100 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));
....
...


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
 
LVL 2

Assisted Solution

by:Peter-Darton
Peter-Darton earned 100 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?

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

Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
chcp 65001 File encoding 66 274
What is sub-make ? 2 71
delete-remove 14 97
nested if statement in excel help 4 37
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…
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 opening and reading files 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.

860 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