Thread: Passing ponter to function to another function

  1. #1
    Registered User
    Join Date
    Jan 2017
    Posts
    4

    Passing ponter to function to another function

    Started to program in C sometime ago and there is a question that I have not been able to get an answer in google. I'm passing a pointer to a structure to a function, that function calls another function with the same structure. Would this work:
    Code:
    main.c
    struct theStruct{ int a;
             int b;
    } myStruct;
    int main(){
             function1(&myStruc);
    }
    
    menu.c
    void function1(struct theStruct *tempStruct){
             function2(tempStruct);
    }
    
    void function2(struct theStruct *tempStruct){
    
        do something;
    }
    I hope is not too confusing, I have a separate file where I have all the menu functions.

    Thanks for the help.

  2. #2
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Code:
    // menu.h ///////////////////////////////////////
    #ifndef MENU_H_
    #define MENU_H_
    
    typedef struct TheStruct {
        int a;
        int b;
    } TheStruct;
    
    void function1(TheStruct *ts);
    void function2(TheStruct *ts);
    
    #endif
    
    
    // main.c ///////////////////////////////////////
    
    #include "menu.h"
    
    int main() {
        TheStruct ts = {1, 2};
        function1(&ts);
        return 0;
    }
    
     
    // menu.c ///////////////////////////////////////
    
    #include "menu.h"
    #include <stdio.h>
    
    void function1(TheStruct *ts) {
        function2(ts);
    }
     
    void function2(TheStruct *ts) {
        printf("%d %d\n", ts->a, ts->b);
    }

  3. #3
    Registered User
    Join Date
    Jan 2017
    Posts
    4
    Thanks very much.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing the address of a function to a function.
    By SqueezyPeas in forum C Programming
    Replies: 2
    Last Post: 09-17-2015, 07:42 AM
  2. Passing variable from function - main - function
    By ulti-killer in forum C Programming
    Replies: 2
    Last Post: 11-01-2012, 12:14 PM
  3. help with passing a pointer to a function to another function
    By csepraveenkumar in forum C Programming
    Replies: 5
    Last Post: 03-20-2011, 01:13 PM
  4. Replies: 1
    Last Post: 09-04-2007, 10:00 PM
  5. Passing values from function to function
    By itld in forum C++ Programming
    Replies: 2
    Last Post: 10-24-2001, 07:28 AM

Tags for this Thread