Thread: Passing members of structs to functions

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    9

    Passing members of structs to functions

    How I pass a member of a struct to a function? I can't pass the whole struct because this isn't one of my own functions. Any ideas?

  2. #2
    Samuel shiju's Avatar
    Join Date
    Dec 2003
    Posts
    41
    As you pass a normal variable

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    struct my_struct {
      int x;
      int y;
    };
    
    int do_stuff(int x) {
      return 0; /* hey who said it was a useful function */
    }
    
    int main(void) {
      struct my_struct n = {1, 2};
    
      do_stuff(n.x);
    
      return 0;
    }

  4. #4
    Registered User
    Join Date
    Aug 2004
    Posts
    9
    thanks for the replies, but more specifically how do I pass an array of chars in a struct to a function?

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Same idea...but I'm in a helpful mood today.

    Code:
    #define NAME_SIZE  8
    
    struct my_struct {
      char name[NAME_SIZE];
    };
    
    int do_stuff(char *name) {
      printf("what is your name?");
      fgets(name, NAME_SIZE, stdin);
    
      return 0; /* hey who said it was a useful function */
    }
    
    int main(void) {
      struct my_struct n;
    
      do_stuff(n.name);
    
      printf("My name is %n, and I'm addicted to programming.", n.name);
    
      return 0;
    }

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    9
    Thanks a lot. Must be something wrong with my code then but that's WINAPI - another story....

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I am plenty familiar with the WinAPI if you had a more specific question.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:35 AM
  2. Passing structs to callback functions (GTK+ related)
    By Raskalnikov in forum C Programming
    Replies: 2
    Last Post: 03-21-2009, 12:46 PM
  3. Passing functions between files
    By Duskan in forum C Programming
    Replies: 9
    Last Post: 04-17-2007, 07:44 AM
  4. Passing structs
    By MethodMan in forum C Programming
    Replies: 5
    Last Post: 04-05-2003, 11:15 PM
  5. Replies: 4
    Last Post: 04-01-2003, 12:49 AM