Passing Value of a struct into a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gilly
    New Member
    • Mar 2007
    • 6

    Passing Value of a struct into a function

    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.lengt h);

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

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

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

    Void print_from_stru ct{
    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
  • emaghero
    New Member
    • Oct 2006
    • 85

    #2
    Originally posted by gilly
    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.lengt h);

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

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

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

    Void print_from_stru ct{
    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

    Code:
    void FunctionName(struct StructName)
    {
           //Function Body
    
           printf("The length is %e \n",StructName.length);
    
          //Functions of type void don't require a return type
    }

    Comment

    • svlsr2000
      Recognized Expert New Member
      • Feb 2007
      • 181

      #3
      Originally posted by gilly
      void input(){
      Input input;


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

      Void print_from_stru ct{
      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.

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by emaghero
        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

        Code:
        void FunctionName(struct StructName)
        {
               //Function Body
        
               printf("The length is %e \n",StructName.length);
        
              //Functions of type void don't require a return type
        }
        Other way is to make structre global.

        //typedef a struct

        StructName MyStruct.

        //main and rest of the functions and coding

        Savage

        Savage

        Comment

        Working...