Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > functio to load struct

Reply
Thread Tools

functio to load struct

 
 
Bill Cunningham
Guest
Posts: n/a
 
      06-06-2010
This is what I've been trying to accomplish. It compiles into an object
file with no complaints so there must be something right. I wanted a
function to load up a pointer to a struct.

#include "c.h"

struct cat *load(char *name, char *color)
{
struct cat *temp;
temp->color = color;
temp->name = name;
return temp;
}

Bill


 
Reply With Quote
 
 
 
 
Sjouke Burry
Guest
Posts: n/a
 
      06-06-2010
Bill Cunningham wrote:
> This is what I've been trying to accomplish. It compiles into an object
> file with no complaints so there must be something right. I wanted a
> function to load up a pointer to a struct.
>
> #include "c.h"
>
> struct cat *load(char *name, char *color)
> {
> struct cat *temp;
> temp->color = color;
> temp->name = name;
> return temp;
> }
>
> Bill
>
>

???? A pointer returned which exists as automatic memory?
And which does not point anywhere?
It can die before you can use it, and points to a random place.
How about:
struct cat *load(char *name, char *color)
{
static struct cat temp;
temp.color = color;
temp.name = name;
return &temp;
}
Now temp stays alive, and you return a pointer to it.
and temp stays valid until you use the function "load" again.
 
Reply With Quote
 
 
 
 
Bill Cunningham
Guest
Posts: n/a
 
      06-06-2010
Sjouke Burry wrote:

> ???? A pointer returned which exists as automatic memory?
> And which does not point anywhere?
> It can die before you can use it, and points to a random place.
> How about:
> struct cat *load(char *name, char *color)
> {
> static struct cat temp;
> temp.color = color;
> temp.name = name;
> return &temp;
> }
> Now temp stays alive, and you return a pointer to it.
> and temp stays valid until you use the function "load" again.


I got this from kandr2 6. on structs. There were no pointers in the
code like I used. The code looked more like yours with members to the
struct. But it was return temp rather than &temp.

Bill



 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      06-06-2010
Sjouke Burry wrote:

> ???? A pointer returned which exists as automatic memory?
> And which does not point anywhere?
> It can die before you can use it, and points to a random place.
> How about:
> struct cat *load(char *name, char *color)
> {
> static struct cat temp;
> temp.color = color;
> temp.name = name;
> return &temp;
> }
> Now temp stays alive, and you return a pointer to it.
> and temp stays valid until you use the function "load" again.


Why the static within the function? Isn't struct cat temp local to the
function?

Bill


 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      06-06-2010
On 6 June, 19:22, "Bill Cunningham" <(E-Mail Removed)> wrote:
> Sjouke Burry wrote:


> > ???? A pointer returned which exists as automatic memory?


not necessarily wrong.

> > And which does not point anywhere?


that's the problem. A pointer is just the address of a piece of
memory. Like any other variable it has to be initialised. You need to
point the pointer at something. You could use malloc() or a static
(statics have there own problems though)

> > It can die before you can use it, and points to a random place.
> > How about:
> > struct cat *load(char *name, char *color)
> > *{
> > * * *static struct cat temp;
> > * * *temp.color = color;
> > * * *temp.name = name;
> > * * *return &temp;
> > *}
> > Now temp stays alive, and you return a pointer to it.
> > and temp stays valid until you use the function "load" again.

>
> * * I got this from kandr2 6. on structs.


no you didn't. K&R may not be perfect but it doesn't make boo-boos
like this.

> There were no pointers in the
> code like I used.


quite

> The code looked more like yours with members to the
> struct. But it was return temp rather than &temp.


 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      06-06-2010
Nick Keighley wrote:

[snip]

> that's the problem. A pointer is just the address of a piece of
> memory. Like any other variable it has to be initialised. You need to
> point the pointer at something. You could use malloc() or a static
> (statics have there own problems though)


So you are saying use malloc() rather than static? How would you use
malloc() in this case?

Bill


 
Reply With Quote
 
Sjouke Burry
Guest
Posts: n/a
 
      06-06-2010
Bill Cunningham wrote:
> Sjouke Burry wrote:
>
>> ???? A pointer returned which exists as automatic memory?
>> And which does not point anywhere?
>> It can die before you can use it, and points to a random place.
>> How about:
>> struct cat *load(char *name, char *color)
>> {
>> static struct cat temp;
>> temp.color = color;
>> temp.name = name;
>> return &temp;
>> }
>> Now temp stays alive, and you return a pointer to it.
>> and temp stays valid until you use the function "load" again.

>
> I got this from kandr2 6. on structs. There were no pointers in the
> code like I used. The code looked more like yours with members to the
> struct. But it was return temp rather than &temp.
>
> Bill
>
>
>

struct cat *load( tells us you want to return a pointer,
temp is a structure, &temp is a pointer to that.
 
Reply With Quote
 
Sjouke Burry
Guest
Posts: n/a
 
      06-06-2010
Bill Cunningham wrote:
> Sjouke Burry wrote:
>
>> ???? A pointer returned which exists as automatic memory?
>> And which does not point anywhere?
>> It can die before you can use it, and points to a random place.
>> How about:
>> struct cat *load(char *name, char *color)
>> {
>> static struct cat temp;
>> temp.color = color;
>> temp.name = name;
>> return &temp;
>> }
>> Now temp stays alive, and you return a pointer to it.
>> and temp stays valid until you use the function "load" again.

>
> Why the static within the function? Isn't struct cat temp local to the
> function?
>
> Bill
>
>

Yes, and you cant use a local variable outside the function.
The static keyword makes temp a permanent one, and allows a
pointer to it to be used outside the funtion "load".
 
Reply With Quote
 
Nobody
Guest
Posts: n/a
 
      06-06-2010
On Sun, 06 Jun 2010 14:50:56 -0400, Bill Cunningham wrote:

>> that's the problem. A pointer is just the address of a piece of
>> memory. Like any other variable it has to be initialised. You need to
>> point the pointer at something. You could use malloc() or a static
>> (statics have there own problems though)

>
> So you are saying use malloc() rather than static? How would you use
> malloc() in this case?


I'd suggest making the caller perform the allocation, and passing the
pointer in, i.e.:

void load(struct cat *result, char *name, char *color)
{
result->color = color;
result->name = name;
}

Returning a pointer to a static variable is poor practice (it means that
the function isn't re-entrant, so can't be used in multi-threaded code,
signal handlers, etc). Using malloc() is inefficient.

Having the caller perform the allocation is more flexible, and allows for
arrays of structures.

 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      06-06-2010
Nobody wrote:

> I'd suggest making the caller perform the allocation, and passing the
> pointer in, i.e.:
>
> void load(struct cat *result, char *name, char *color)
> {
> result->color = color;
> result->name = name;
> }
>
> Returning a pointer to a static variable is poor practice (it means
> that the function isn't re-entrant, so can't be used in
> multi-threaded code, signal handlers, etc). Using malloc() is
> inefficient.
>
> Having the caller perform the allocation is more flexible, and allows
> for arrays of structures.


So the pointer result contains all the data that has been passed to the
2nd and 3rd parameters then right?

Bill


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
obtain value from functio + array Don Quijote de Nicaragua ASP .Net 1 07-27-2008 01:56 PM
Differences between Functio & Macro in C uma676 C Programming 13 07-06-2005 03:53 AM
Copy functio in imaplib Raghul Python 2 02-22-2005 03:42 PM
struct my_struct *p = (struct my_struct *)malloc(sizeof(struct my_struct)); Chris Fogelklou C Programming 36 04-20-2004 08:27 AM
template functio need help Rex_chaos C++ 7 10-07-2003 11:46 AM



Advertisments