Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

function returning struct 1

Status
Not open for further replies.

svar

Programmer
Aug 12, 2001
349
GR
Ok, well a function can only return a scalar, so a pointer to the function
But I want to say
mystruct= *(myfunction(mystruct,....))

my_structure *myfunction(my_structure mystruct,....){
return(&mystruct); }


why does not this work and how can I accomplish it WITHOUT
passing &mystruct to the function
?
 
> Ok, well a function can only return a scalar,
Functions can return structs - so long as your compiler is an ANSI compiler.
If it's some old fossil from before the time a 286 seemed like a really good processor, you may have problems.

Code:
typedef struct {
    int frequency;
    int length;
} foo_st;

foo_st func ( void ) {
    foo_st result = { 1, 2 };
    return result;
}

Then you just say
somevar = func();

--
 
So much for old K&R. So the only thing a function does not return is an array, right?
 
> So much for old K&R
Yeah, the one without a big [tt]ANSI C[/tt] stamped on the front is not really appropriate for modern C programming.

> So the only thing a function does not return is an array, right?
Correct, you can't return an array as a value
But you can always return a pointer to an array

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top