?
Solved

How to pass back to caller function location of reallocated arrays?

Posted on 2006-05-09
3
Medium Priority
?
188 Views
Last Modified: 2010-04-15


Hi All,

I want to write a C function that takes in input two arrays, may do some memory re-allocation, put some data into them and return the pointers to the two (possibly moved) arrays so that they are available to the calling function. The prototype should be like:

AddItemToArrays(char * Array1, char * Array2, char * Item1, char * Item2);

The problem I have is that when the Array1 and Array2 need to be re-allocated, the memory location will change and I am not sure how best to set this up so that the calling function will know about the new locations.

can you suggest what's the best way to do this?

Thanks,
Stefano
0
Comment
Question by:sdamiano
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 1500 total points
ID: 16642729
Hi sdamiano,

The normal way to deal with this situation is to pass pointers to the pointers or put both in a struct and pass a pointer to the struct.

AddItemToArrays(char * Array1, char * Array2, char * Item1, char * Item2);

becomes

AddItemToArrays(char ** Array1, char ** Array2, char * Item1, char * Item2);

or perhaps

typedef struct ArrayPair
{
  char * array1;
  char * array2;
} ArrayPair;

AddItemToArrays(ArrayPair * pair, char * Item1, char * Item2);

or even perhaps:

AddItemToArrays(ArrayPair * pair, ArrayPair item);

Paul
0
 

Author Comment

by:sdamiano
ID: 16643219
Hi Paul,

ok, that's what I did, it took me a little while to figure out how to deal with the double pointers, but it's working well now.

Thanks,
Stefano
0
 
LVL 16

Expert Comment

by:PaulCaswell
ID: 16643563
Hi sdamiano,

They're quite tricky 'till you get your head around them. :-)

Have a good day!

Paul
0

Featured Post

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.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops 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.

612 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