return type of a function

D

Darshan

What should be the return type of a function returning an array of
pointers to a structure?
 
E

Eric Sosman

Darshan said:
What should be the return type of a function returning an array of
pointers to a structure?

No such function is possible in C, so its "return type"
is a type from some other language.

A C function might return a pointer to the first element
of an array. If the array's elements are of type `struct foo*',
a pointer to one of them (and the function's type) would be
`struct foo**'.
 
D

Darshan

     No such function is possible in C, so its "return type"
is a type from some other language.

     A C function might return a pointer to the first element
of an array.  If the array's elements are of type `struct foo*',
a pointer to one of them (and the function's type) would be
`struct foo**'.

will not it result into pointer dangling?
bcoz the function stack frame will be deleted.
 
T

Tim Harig

will not it result into pointer dangling?
bcoz the function stack frame will be deleted.

It will if it is a local variable to the called function.

In C, you cannot return more then one of any given data type back to the
calling function. An array is not a datatype but instead it is a group
of any other datatypes all allocated together consecutively in memory. A
function can only return one or a pointer to one back to the calling
function.

Generally arrays are passed given there first element as it is then
possible to index a known quantity of others from the first element.
In other structures such as doubly linked lists, it may not matter which
element you send back.

All of this gives you two options for returning your data:

1. You can allocate the array on the heap using malloc(). Then you can
safely return a pointer to the first element of the array without
breaking the stack frame. I would consider this to be the best
option (especially for large arrays that could create a stack
overflow).

2. You can allocate memory for the array in the calling function. Then you
can pass a pointer to the first elment to the called function
along with size information if necessary. The called function
will save its results directly to the passed array.

It will potentially overight any data that had been saved be the
calling function so the calling function should make copies of an
arrays data if necessary before passing it.

This can also be difficult as the calling function must know the size
of the array needed before calling the function which is not often
the case; and, one must take extra caution to make sure that the
called function does not overun the array.

This can be done; but, I don't normally recommend it as there is a
greater potential for worse errors.
 
P

Peter Nilsson

[email protected] (Richard Harter) said:
Not directly, perhaps, but can't we wrap the array in a struct?

Returning potentially large structs is generally frowned upon,
though that is an implementation detail.
Modulo syntax errors something like

struct hack {
    struct tgt *a[5];
};

struct hack foo(void) {
   struct hack yecch;
   /* Some sort of code */

If your 'struct hack' is a struct hack and you intent to return
more than 5 struct tgt pointers, realise you've only allocated
sizeof(struct hack) bytes. You can't write to yecch beyond
that limit.
   return yecch;

Nor can you return more than that limit.
 
T

Tim Harig

No such function is possible in C, so its "return type"
is a type from some other language.
Not directly, perhaps, but can't we wrap the array in a struct?

struct hack {
struct tgt *a[5];
};

Unless the user is working with a special kind of data throughout his
program where it makes sense to create a datatype for it, he would be
just using the structure as a one off kludge to bypass C's rules. It may
follow the letter of the language; but, not the intention. It would be
rather poor programming.

User created data types were designed to help convey the logical
structure of data within a program. A structure should coorispond to a
real conceptual entity just like objects in an object oriented language
should model real data relationships. Doing so helps to clarify your
program -- not doing so ofiscates it.

Also, using a structure to wrap arrays of large sizes (which cannot be
resized within the confines of the structure) will have implications on the
stack. This can lead to poor performance as values must be copied between
function operations and in stack overflows for large enough structures.
Large datasets are much better suited to being allocated on the heap.
 
T

Tim Harig

If your 'struct hack' is a struct hack and you intent to return
more than 5 struct tgt pointers, realise you've only allocated
sizeof(struct hack) bytes. You can't write to yecch beyond
that limit.

Nor can you operationally reduce the size of the array in your structure
if you find that you are not making efficient use of it.
 
E

Eric Sosman

Richard said:
No such function is possible in C, so its "return type"
is a type from some other language.

Not directly, perhaps, but can't we wrap the array in a struct?
[...]

Yes, but then the function would return a struct, not an
array. You could also wrap an array in a union, but then the
function would return a union. You could wrap an array inside
a union inside a struct that's a one-element array inside yet
another union, but then the function would return -- oh,
whatever that mess would be, but not an array.

No C function can return an array.
 
T

Tim Rentsch

Eric Sosman said:
No C function can return an array.

There is one rather weak sense in which a C function can return
an array. A function can return a (double _Complex), which is
isomorphic to an array of type (double[2]).

This doesn't change the truth of Eric's statement. I just
thought the existence of this corner case makes an interesting
observation.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top