Pointers/Arrays of structures anyone?? 
Author Message
 Pointers/Arrays of structures anyone??

I have just opened a huge can of worms on the best, most readable and
efficient way to solve the following problem:

Basically, a function is called which can return either:

a) an array of structures or
b) a single structure

of the type, say:

struct {
char name[20];
char street[20];
char city[20];

Quote:
}

When calling the function another variable is passed which will be
either a specific 'name' or the character string "ALL", thus the
function will return either the structure for the name entered or an
array of structures for all names. It is not known how many names will
be returned using "ALL", though the MAXIMUM number of names is known.

The question is:
What is the best way to allocate and handle the memory for maximum
efficiency? Will it be easier to allocate and clear space for the
maximum number of records and just return an array of structures, even
if there is only one? Or will some clever pointer stuff be more
efficient?


today.



Sun, 29 Jul 2001 03:00:00 GMT  
 Pointers/Arrays of structures anyone??
First of all, a function can return only one value either a single
structure or an array of structure.

-- Baliga

Quote:

> I have just opened a huge can of worms on the best, most readable and
> efficient way to solve the following problem:

> Basically, a function is called which can return either:

> a) an array of structures or
> b) a single structure

> of the type, say:

> struct {
> char name[20];
> char street[20];
> char city[20];
> }

> When calling the function another variable is passed which will be
> either a specific 'name' or the character string "ALL", thus the
> function will return either the structure for the name entered or an
> array of structures for all names. It is not known how many names will
> be returned using "ALL", though the MAXIMUM number of names is known.

> The question is:
> What is the best way to allocate and handle the memory for maximum
> efficiency? Will it be easier to allocate and clear space for the
> maximum number of records and just return an array of structures, even
> if there is only one? Or will some clever pointer stuff be more
> efficient?


> today.



Sun, 29 Jul 2001 03:00:00 GMT  
 Pointers/Arrays of structures anyone??
: I have just opened a huge can of worms on the best, most readable and
: efficient way to solve the following problem:

: Basically, a function is called which can return either:

: a) an array of structures or
: b) a single structure

Well then, the function actually returns (a pointer to) an array of
structures, of length N.

: of the type, say:

: struct {
: char name[20];
: char street[20];
: char city[20];
: }

: When calling the function another variable is passed which will be
: either a specific 'name' or the character string "ALL", thus the
: function will return either the structure for the name entered or an
: array of structures for all names. It is not known how many names will
: be returned using "ALL", though the MAXIMUM number of names is known.

Then you are going to have to allocate memory in the function, and
return a pointer to the array.

: The question is:
: What is the best way to allocate and handle the memory for maximum
: efficiency? Will it be easier to allocate and clear space for the
: maximum number of records and just return an array of structures, even
: if there is only one? Or will some clever pointer stuff be more
: efficient?

No, just malloc enough memory for however many elements you are
returning.  You can either add a terminating marker at the end of
the array, or pass a pointer to the array size to the function
and update this before exiting.  (Or various permutations of the
above).  Don't forget to free the memory in the main program.

Will



Sun, 29 Jul 2001 03:00:00 GMT  
 Pointers/Arrays of structures anyone??

Quote:

>I have just opened a huge can of worms on the best, most readable and
>efficient way to solve the following problem:

>Basically, a function is called which can return either:

>a) an array of structures or
>b) a single structure

>of the type, say:

>struct {
>char name[20];
>char street[20];
>char city[20];
>}

>When calling the function another variable is passed which will be
>either a specific 'name' or the character string "ALL", thus the
>function will return either the structure for the name entered or an
>array of structures for all names. It is not known how many names will
>be returned using "ALL", though the MAXIMUM number of names is known.

How big is this maximum? If it isn't excessive you could allocate
an array of strcutures in the caller and pass a pointer to the first
element. Your function can fill up as many as it needs and the return
value of the function can be the number of elements it wrote to
(which will be 1 with a specific name, or perhaps 0 if there is no match).

--
-----------------------------------------


-----------------------------------------



Tue, 31 Jul 2001 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Array of Structures anyone??

2. An array in a structure Or an Array Of Structures

3. pointers, structures, and arrays....

4. structure array pointer difficulties

5. Pointers in C and arrays of structures

6. Array of structures and pointers (again)

7. pointers, structures, and arrays

8. Returning pointer to array of structure from function

9. A question about arrays,pointers and structures

10. pointers within an array of structures

11. Pointer to an array of structures

12. structure array pointer difficulties

 

 
Powered by phpBB® Forum Software