#### 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.

## 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 on other sites
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 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 on other sites
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;}

PERSON People[3];UpdatePeeps(People);

Because an array is really a pointer...

##### Share on other sites
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 on other sites

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 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 on other sites
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 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 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 on other sites
How could you think "use function that ''malloc''"?

##### 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...

• 45
• 11
• 17
• 11
• 13