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!

Pass the pointer of an item in a struct to a function 3

Status
Not open for further replies.

dickiebird

Programmer
Feb 14, 2002
758
GB
Hi Guys
I want to pass the pointer of a struct item, to a function :
Which do I use ( and why, if you've time to explain) ?

1. format_group_code(&rec->agent_group);
2. format_group_code(&rec.agent_group);
3. format_group_code(rec->agent_group);


typedef struct
{
char header[4];
char agent_code[6];
char old_s4[3];
char new_s4[3];
} B1_REC;

format_group_code(char *group_code)
{
char tmp_group1[6], tmp_group2[6], *cp;
memcpy(tmp_group1, group_code, sizeof(tmp_group1) - 1);
.
.
.
return
}

TIA
Dickie Bird (:)-)))
 
Hi DB,

try starting with this FAQ then re-post if you still have questions faq205-2913

good luck
-pete
 
Hi
it seems to 3rd option is more correct, that is

format_group_code(rec->agent_group);
 
Hi Dickie!

I think you should just pass to the function the address of the structure. Then locally you'll be able to access any memory location for the members of the structure.


void format_group_code(B1_REC *rec) {
gets(rec->agent_group);
}

void main {

BI_REC x;

puts("Agent Group :");
format_group_code(BI_REC &x);

}
 
I don't think this question can be answered fully.
You give 3 possibilities at the start of your post namely:

1. format_group_code(&rec->agent_group);
2. format_group_code(&rec.agent_group);
3. format_group_code(rec->agent_group);

It all depends on how the rec variable is declared /defined. As this definition/declaration is not given I'm not sure if a definite answer can be given.

By The way,
what is that *cp pointer up to?
Also why when you do the memcpy do you copy one bite less. If you are dealing with strings then this could be a problem.
 
guestgulkak - the code of the function isn't particularly relevant to my question, but, for info. - group codes are always 4 chars and B1_REC is used for other codes up to 6 chars.
I should have included the defination of rec :
B1_REC rec;
You're right - it could have been : B1_REC *rec; - in which case I'd use : format_group_code(&rec.agent_group);
(I think)
In the end - my compiler gave a warning for all except: format_group_code(rec->agent_group);
Thanks for your advice, everyone !
Dickie Bird (:)-)))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top