• Advertisement

Archived

This topic is now archived and is closed to further replies.

pointing to structs

This topic is 5561 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, This is an edited post as I solved my own problem (gosh I can be stupid!) My *new* question is "Is this a safe way to do what I'm doing?" and is it correct? (should I use return instead? if so how?)... I want to make sure there are no memory leaks mainly. I have a structure like so struct PERSON { int age; char name[11]; } and I create an array like so PERSON Peep[3]; and I want a function to fill out the details so I do this: void UpdatePeeps(PERSON * Peep) { strcpy(Peep[0].name, "Fred"); Peep[0].age = 20; strcpy(Peep[1].name, "JOE"); Peep[1].age = 10; strcpy(Peep[2].name, "BILL"); Peep[2].age = 29; } Thanks sorry about the edit [edited by - redoc on December 4, 2002 11:30:47 PM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster

void UpdatePeeps(PERSON * Peep)
{
strcpy(Peep->name, "Fred";
Peep->age = 20;
}


when you call the function be sure to use the & if the struct you are passing isn''t a pointer ie

PERSON Fred;
UpdatePeeps(&Fred);

Share this post


Link to post
Share on other sites
Thanks! and WOW what a quick reply! though you have replied to my message before my edit (oops)

is the code I have safe to use? it appears to work as it is now.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Whoops, sorry homes, I didn''t realize you wanted to alter an array of PEOPLE. Here, use this (maybe you already figured this out):

void UpdatePeeps(PERSON * Peep)
{
strcpy(Peep[0]->name, "Fred";
Peep[0]->age = 20;
strcpy(Peep[1]->name, "JOE";
Peep[1]->age = 10;
strcpy(Peep[2]->name, "BILL";
Peep[2]->age = 29;
}

Then in your main code:

PERSON People[3];
UpdatePeeps(People);

Because an array is really a pointer...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''m retarded, I didn''t even read your edit. Yes it''s safe, there''s no need for a return. If you use new make sure to use delete and if you use malloc use free, but other than that you''ll be fine...

Share this post


Link to post
Share on other sites
Thanks again for your prompt reply!

However, it doesnt compile when I change the "."''s to "->"''s

The code I posted above actually does work. I would like to confirm the questions:

Is what I have done te correct way to do it

Should I use return instead? if so how?

and finally I want to make sure there are no memory leaks.

Thank you

Share this post


Link to post
Share on other sites
Its amazing you reply faster than I can even write

(also sorry I can imagine this gets asked all the time too.. I tried reading up)

so what is the use in return, if you can alter the data as a parameter?

just curious

thanks lots

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yeah, again I''m retarded, sorry, just use "."''s not "->" in this example. You can use return to return from a function before the actual end of the function. This could be useful to skip unneeded instructions. Say you want to draw a bitmap but you are given coordinates that are negative. Consider the following function:

void BlitThing(int x, int y)
{
if(x < 0 || y < 0)
return;
// Now blit the thing because we know the coordinates
// are valid
// [...]
}

Share this post


Link to post
Share on other sites
I see, I knew to return for the "fast" exit..

but having thought about it I imagine the use of return is to beable to make your commands easier.

like a return of a bool,

if (Initialization())
{
// init returned true so do something here
{
else
{
// init returned false so quit
}

Thanks, I''ll get the hang of it all soon

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster


void UpdatePeeps(PERSON * Peep)
{
strcpy(Peep[0]->name, "Fred");
Peep[0].age = 20;
strcpy(Peep[1]->name, "JOE");
Peep[1].age = 10;
strcpy(Peep[2]->name, "BILL");
Peep[2].age = 29;
}




dude that's not safe. there is no way UpdatePeeps can know how big Peep is. if the array you pass in only has 2 elements then when you do stuff to Peep[2] you are writing to some random part of memory. sure, maybe you'll just make sure that you always pass an array of at least 3 elements but it could be a source of errors if you're not careful. also you don't check the lengths of the names so if you had a really long name it would write past the end of the string and cause problems.

edit: broken code tags

[edited by - necromancer_df on December 4, 2002 12:16:05 AM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
How could you think "use function that ''malloc''"?

Share this post


Link to post
Share on other sites
void UpdatePeeps(PERSON * Peep)
{
strcpy(Peep[0].name, "Fred");
Peep[0].age = 20;
strcpy(Peep[1].name, "JOE");
Peep[1].age = 10;
strcpy(Peep[2].name, "BILL");
Peep[2].age = 29;
}

wouldn''t be better to do it like this?

void UpdatePeeps (PERSON people[])
{
strcpy(Peep[0].name, "Fred");
Peep[0].age = 20;
strcpy(Peep[1].name, "JOE");
Peep[1].age = 10;
strcpy(Peep[2].name, "BILL");
Peep[2].age = 29;
}

i take it you are doing this in C...

Share this post


Link to post
Share on other sites

  • Advertisement