Thread: how to call a function that takes a STRUCT parameter

  1. #1
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61

    Exclamation how to call a function that takes a STRUCT parameter

    I just want to call the function : outputboo(), but I dont know how

    Code:
    /*Enthusiastic
    Pessimism
    desensitize
    uniqueness
    */
    
    #include <stdio.h>
    #include <string.h>
    
    struct Books{
       char  title[50];
       char  author[50];
       char  subject[100];
       int   book_id;
    };
    struct Books printhello();
    int  outputboo(struct Books boo);
    
    int main( ){
    
    printhello();
    int outputboo();
    
    }
    
        int outputboo(struct Books boo){
        printf(" here is your value of the function of the STRUCT :%i",&boo.book_id );
    
    }
    
          struct Books printhello(){
          struct Books boo ;
          printf("Enter your book # here please :");
          scanf("%i",&boo.book_id);
          printf("your boo book_id is : %i\n\n\n\n\n\n\n\n\n\n", boo.book_id);
          ;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You really need to indent your code properly. For example:
    Code:
    /* Enthusiastic
       Pessimism
       desensitize
       uniqueness
    */
     
    #include <stdio.h>
    #include <string.h>
    
    struct Books {
        char  title[50];
        char  author[50];
        char  subject[100];
        int   book_id;
    };
    
    struct Books printhello();
    int outputboo(struct Books boo);
    
    int main() {
        printhello();
        int outputboo();
    }
    
    int outputboo(struct Books boo) {
        printf(" here is your value of the function of the STRUCT :%i",&boo.book_id );
    }
    
    struct Books printhello() {
        struct Books boo;
        printf("Enter your book # here please :");
        scanf("%i", &boo.book_id);
        printf("your boo book_id is : %i\n\n\n\n\n\n\n\n\n\n", boo.book_id);
        ;
    }
    As for your problem, I suggest the use of a pointer, e.g.,
    Code:
    void print_book(const struct Books *book) {
        printf(" here is your value of the function of the STRUCT :%i", book->book_id);
    }
    Now you can call it:
    Code:
    struct Books book;
    book.book_id = 123;
    print_book(&book);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61
    Quote Originally Posted by laserlight View Post
    You really need to indent your code properly. For example:
    Code:
    /* Enthusiastic
       Pessimism
       desensitize
       uniqueness
    */
     
    #include <stdio.h>
    #include <string.h>
    
    struct Books {
        char  title[50];
        char  author[50];
        char  subject[100];
        int   book_id;
    };
    
    struct Books printhello();
    int outputboo(struct Books boo);
    
    int main() {
        printhello();
        int outputboo();
    }
    
    int outputboo(struct Books boo) {
        printf(" here is your value of the function of the STRUCT :%i",&boo.book_id );
    }
    
    struct Books printhello() {
        struct Books boo;
        printf("Enter your book # here please :");
        scanf("%i", &boo.book_id);
        printf("your boo book_id is : %i\n\n\n\n\n\n\n\n\n\n", boo.book_id);
        ;
    }
    As for your problem, I suggest the use of a pointer, e.g.,
    Code:
    void print_book(const struct Books *book) {
        printf(" here is your value of the function of the STRUCT :%i", book->book_id);
    }
    Now you can call it:
    Code:
    struct Books book;
    book.book_id = 123;
    print_book(&book);
    sorry for the confusion I caused by not indenting my Code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 191
    Last Post: 08-13-2014, 11:54 AM
  2. C++ Function takes File Object as Reference Parameter
    By noob_coder in forum C++ Programming
    Replies: 4
    Last Post: 02-22-2013, 01:19 AM
  3. Replies: 4
    Last Post: 10-03-2011, 06:30 AM
  4. Array of struct as function parameter
    By rotger in forum C Programming
    Replies: 12
    Last Post: 11-11-2010, 02:33 PM
  5. function that takes struct?
    By pode in forum C++ Programming
    Replies: 11
    Last Post: 07-04-2002, 06:55 PM