By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,385 Members | 1,763 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,385 IT Pros & Developers. It's quick & easy.

Passing Value of a struct into a function

P: 6
Hi,

just a quick question i have a function saving values into my struct, that all works but i do not know how to access these values in a different function to the one that saved them code is below.

typedef struct InputT{
double length;
double startingangle;
double angle;
} Input;

void input(){
Input input;

scanf("%lf", &input.length);

scanf("%lf", &input.startingangle);

scanf("%lf", &input.angle);
return;
}

i wish to now access them in another function say to print
eg

Void print_from_struct{
printf("%lf", input.length);
return;
}

I know this dosnt work but i wish to do something like this any help would be great

Advanced thanks
Gilly
May 24 '07 #1
Share this Question
Share on Google+
3 Replies


emaghero
P: 85
Hi,

just a quick question i have a function saving values into my struct, that all works but i do not know how to access these values in a different function to the one that saved them code is below.

typedef struct InputT{
double length;
double startingangle;
double angle;
} Input;

void input(){
Input input;

scanf("%lf", &input.length);

scanf("%lf", &input.startingangle);

scanf("%lf", &input.angle);
return;
}

i wish to now access them in another function say to print
eg

Void print_from_struct{
printf("%lf", input.length);
return;
}

I know this dosnt work but i wish to do something like this any help would be great

Advanced thanks
Gilly

Once your struct is defined you can treat it like any other variable of known type, eg int, double etc. Your function declaration should be done as normal. Put the struct as a parameter in the function header. Once inside the function you can cast to a double and print using printf or use cout. It should look like

Expand|Select|Wrap|Line Numbers
  1. void FunctionName(struct StructName)
  2. {
  3.        //Function Body
  4.  
  5.        printf("The length is %e \n",StructName.length);
  6.  
  7.       //Functions of type void don't require a return type
  8. }
  9.  
May 24 '07 #2

Expert 100+
P: 181
void input(){
Input input;


i wish to now access them in another function say to print
eg

Void print_from_struct{
printf("%lf", input.length);
return;
}
just a small reminder, here u would get a error for redefiniton of input, there are few limitations in naming variables. you can always pass structures by value.
May 24 '07 #3

Savage
Expert 100+
P: 1,764
Once your struct is defined you can treat it like any other variable of known type, eg int, double etc. Your function declaration should be done as normal. Put the struct as a parameter in the function header. Once inside the function you can cast to a double and print using printf or use cout. It should look like

Expand|Select|Wrap|Line Numbers
  1. void FunctionName(struct StructName)
  2. {
  3.        //Function Body
  4.  
  5.        printf("The length is %e \n",StructName.length);
  6.  
  7.       //Functions of type void don't require a return type
  8. }
  9.  
Other way is to make structre global.

//typedef a struct

StructName MyStruct.

//main and rest of the functions and coding

Savage

Savage
May 24 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.