Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • Last Modified:

Passing Structure member to a function in another file.

Dear experts

I am modifying the source code written by previous programmer
In the code, I have header file named session.h
in which one of structure member declared as
typedef struct sesh

{

            char usr_id[9 + 1];
} SESH;

Now i am passing this member to a another c file named message.c so that e_usr_id will contain the variable stored in
usr_id as follows


in message.c
in function
int PopulateMessage()
{

strcpy(bp[no_Msg]->e_usr_id, usr_id );

}

in above code e_usr_id comes from #include <cust.h>
Apart from adding #include<session.h> in message.c
will there be any other changes required
How aboout the use of sprintf() instead of strcpy()

Thanks for your time

Regards
Ronan
0
ronan_40060
Asked:
ronan_40060
  • 5
  • 4
1 Solution
 
AxterCommented:
Hi ronan_40060,
> >strcpy(bp[no_Msg]->e_usr_id, usr_id );

In the above code, you're not referencing the sesh type.
usr_id, should have a prefix pointing to an object of SESH type.

I don't see a need for using sprintf, and it would be less efficient compared to using strcpy

David Maisonave :-)
Cheers!
0
 
AxterCommented:
ronan_40060,
> >will there be any other changes required

To use strcpy, you should have an #include <string.h>

David Maisonave :-}
0
 
ronan_40060Author Commented:
Hi David,

Thanks for you valuable input
I did not understand when you mentions
>> In the above code, you're not referencing the sesh type.
usr_id, should have a prefix pointing to an object of SESH type.

Please explain
Thanks for your time
regards
ronan

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AxterCommented:
In your code, usr_id is a member of SESH.
If you want to copy usr_id, you need to declare an instance of SESH, and then copy it's member.

SESH my_sesh;
//Initialize my_sesh with data

//Then
strcpy(bp[no_Msg]->e_usr_id, my_sesh.usr_id );
0
 
ronan_40060Author Commented:
Thanks David
Please correct me if I am wrong
I am declaring an instance of SESH in message.c
since structure  SESH is from session.h
while declaring an instance of SESH in message.c it has be declared as extern right ?

 
0
 
ronan_40060Author Commented:
like
extern SESH my_sesh:
0
 
AxterCommented:
>>while declaring an instance of SESH in message.c it has be declared as extern right ?

It needs to be declared extern in your header file, and not in your *.c file.

//session.h
typedef struct sesh
{
          char usr_id[9 + 1];
} SESH;

extern SESH my_sesh;

//message.c
#include "session.h"
SESH my_sesh;

FYI:
The include files that are part of your project should have a "" double quote wrap around it when doing an #include.
The files that are part of the compiler/implementation should have a <> wrap around it.
Example:
#include "session.h"
#include "message.h"
#include <string.h>
#include <stdlib.h>
0
 
ronan_40060Author Commented:
Thanks David
I will get back to you with the results
Thanks for your time
Have a great day
Ronan
0
 
brettmjohnsonCommented:
I also suggest you use strncpy() rather than strcpy(), since your usr_id buffer is only 10 bytes.
0
 
ronan_40060Author Commented:
Hi Kent and brettmjohnson
there are bit of  changes in code that will be modified
There are two tasks that need to be aaccomplished

1> We have a variable named acct_usr_id which stores 7 digit code and I need to append string "P" to acct_usr_id in message.c
and store it to a character array str which is declared as char str[10]; in message.c
2> copy str to e_usr_id

and above both steps need to be implemented the the code below    

ptemp = getparam("usr_idn", 0 );
if (ptemp)
{
      strcpy(e_new_acct_grp, "001");
      // code to store value of acct_usr_id and append string "P" to char array str
      strcpy(e_usr_id, str);      
      audmsg("User id in message25 is %s", ptemp);
}
else
strcpy(e_new_acct_grp, "000");

Please let me know
Regards
Ronan
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now