Thread: structures

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    28

    structures

    I have a structure declared in main and I want this to be avaliable to another function so I assume I need to pass a pointer to the other function ? how is this done.

    Code:
    otherfunction(*ptr);
    
    int main ()
    {
       typedef struct test
          {
             char test1[25];
             int test2[20];
          }tst[10];   
    
       otherfunction(*tst)
    }
    that was my attempt but I know its completely wrong any help will be appreciated.

  2. #2
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    EDIT: POSTED TOO EARLY ACCIDENTALLY

    Firstly, it is probably best to keep the structure definition outside of main then create the declaration in main.

    Code:
    /* structure definition */
    struct test {
        char test1[25];
        int test2[20];
    };
    
    /* function prototype */
    int otherfunction(struct test[]);
    
    int main () {
        struct test a[10];
        otherfunction(a);
        return 0;
    }
    This code will pass the actual array of structures created. To create and pass a single structure:

    Code:
    /* structure definition */
    struct test {
        char test1[25];
        int test2[20];
    };
    
    /* function prototype */
    int otherfunction(struct test);
    
    int main () {
        struct test a;
        otherfunction(a);
        return 0;
    }
    Last edited by Zainny; 08-13-2003 at 06:32 AM.
    Beware the fury of a patient man.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    Do I not need to pass a pointer if I wish to edit the contents of the sturcture in the other function?

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    Do I not need to pass a pointer if I wish to edit the contents of the sturcture in the other function?
    No.
    The most common reason for passing a pointer to a structure rather than the structure itself for a function call is to conserve space on the stack.
    Beware the fury of a patient man.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    161
    Originally posted by Zainny
    No.
    The most common reason for passing a pointer to a structure rather than the structure itself for a function call is to conserve space on the stack.
    This is wrong. If you want to modify the contents of the structure in another function, you must pass a pointer. Otherwise, a bitwise copy is done and you'll just be modifying the copy.

    Code:
    /* structure definition */
    struct test {
        char test1[25];
        int test2[20];
    };
    
    /* function prototype */
    int otherfunction(struct *test);
    
    int main () {
        struct test a;
        otherfunction(&a);
        return 0;
    }
    Note the * in the prototype and the & in the function call.

  6. #6
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    Thanks thats what I wanted.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    This is wrong. If you want to modify the contents of the structure in another function, you must pass a pointer. Otherwise, a bitwise copy is done and you'll just be modifying the copy.
    Doh! Just shows, you shouldn't post after a hard night of drinking

    Apologies for the incorrect information Gav D.
    Beware the fury of a patient man.

  8. #8
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    GCC has a problem with

    int otherfunction(struct *test);

    it says error before *. is the * definatly suposed to be there?

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    32
    Should be:
    Code:
    int otherfunction(struct test *);
    Beware the fury of a patient man.

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    161
    Oops, sorry about the misplaced asterisk. Looks like I wasn't paying full attention either.

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    can anyone tell me why GCC give me '29 : invalid type for argument 1 of get response? Dont ask me why I need the pointer for the structure in getresponse, I do I just havent written the code for it yet. Thanks

    Code:
    int sortentries();
    
    struct phonebook
    {
       char Name[25];
       int No[20];
    };
    
    
    int getresponse (struct phonebook *);
    
    int main ()
    {
       int cnt;
       struct phonebook PBook[10];   
       for (cnt=0;cnt<clr;cnt++)
          puts("");
       gotoxy (0,0);
       displaymenu();
       getresponse(*PBook);
       return (0); 
    }
    
    
    
    int displaymenu ()
    {
       puts ("\nP System");
       puts ("-------------------");
       puts ("\n1) Add Entry");
       puts ("2) Delete Entry");
       puts ("3) Find Entry");
       puts ("4) View List");
       puts ("5) Sort Entries");
       puts ("\nQ) Quit");
       puts ("\nEnter Selection");
       return(0);
    }
    
    
    
    int getresponse (struct phonebook *)
    {
       char chrResponse;
       scanf("%c",&chrResponse);
       switch (chrResponse)
          {
          case '1': addentry(); 
                    return(0);   
          case '2': delentry();    
                    return(0);
          case '3': findentry();    
                    return(0);
          case '4': viewlist();    
                    return(0);
          case '5': sortentries();    
                    return(0);
          case 'Q': puts("Quit");    
                    return(1);
          }
    }
    
    int addentry()
    {}
    
    int delentry()
    {}
    
    int findentry()
    {}
    int viewlist()
    {}
    int sortentries()
    {}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  3. Input output with structures
    By barim in forum C Programming
    Replies: 10
    Last Post: 04-27-2004, 08:00 PM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM