Thread: pass structure to function

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    32

    pass structure to function

    ok im messing around with structures again
    and i cant seem to pass the structure to the function
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    struct record_format { 
           char name[BUFSIZ];
           int age;
           }; 
    
    void display(struct record_format);	
    
    
    
    int main(int argc, char *argv[])
    {
      
    
      struct record_format record[] = {
            { "john", 21 },
            { "larry", 22 }
              };
    
      
    display (record);
    
    				
      
    getchar();
    }
    
    /************************************************************************/
    
    void display(struct record_format rec)
    {
         int i;
         for(i=0;i<2;++i)
      printf("name is &#37;s \nage is %d \n", rec.name, rec.age);
    
    }
    24 C:\Dev-Cpp\mainffff.c incompatible type for argument 1 of `display'
    when i add the name and age to the structure is when it wont pass anymore!
    Last edited by tat; 12-02-2007 at 11:43 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    display (record[0]);
    display (record[1]);

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    display() takes a record_format struct, but you are passing an array of record_format structs.
    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

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    And why do you want to pass a struct instead of pointer? passing struct will copy the whole struct contents to the temp var...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    void display(struct record_format* pRec)
    To pass the array via a pointer. Works for both the entire array or just one element.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    54
    Quote Originally Posted by Elysia View Post
    Code:
    void display(struct record_format* pRec)
    To pass the array via a pointer. Works for both the entire array or just one element.
    yes you can use this type of passing...

  7. #7
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    thank you everyone
    that replied. i got it working with a pointer.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    here is my function to take in the struct
    and i want to check if there are any empty
    seats not taken. DR Power seat 21
    Code:
    struct fast_line {           // structure 
           char pass_name[ 30 ];
           int seat_number;
           };
    Code:
    struct fast_line record[] = {    /** fill seats wth passengers **/
            { "DR Power", 21 },
            { "Green Lantern", 2 },
            { "The Flash" , 30 },
            { "Super Girl" , 7 },
            { "Phoenix", 12 },
            { "Clark Kent", 17 },
            { "The Tick" ,4 },
            { "Swamp Thing" , 26 }
            
              };
    Code:
    void show_seats_taken( struct fast_line *rec )
    {
     int i, length;
     int temp;
     length = strlen(rec->pass_name);
         for( i = 0; i < length; i++ )
          printf( "Seat [&#37;i] is assigned to %s \n\n",  rec[ i ].seat_number , rec[ i ].pass_name );
            if( rec[i].seat_number ==' ' ) // this is not working
            {
           temp = rec[i].seat_number;
           printf("seat %i is available\n" , temp );
         }
         }
    so im trying to find the empty spaces in the array of structs. there are 30 available how many are not filled..
    Last edited by tat; 12-04-2007 at 12:29 AM.

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and why you suppose that the number of members in your array is equal to the length of the name of the first passanger? What is the mysterios connection between these two magic numbers?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    32
    Quote Originally Posted by vart View Post
    and why you suppose that the number of members in your array is equal to the length of the name of the first passanger? What is the mysterios connection between these two magic numbers?
    I suppose your talking about this?
    Code:
    length = strlen(rec->pass_name);

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    vart is being sarcastic.
    The length of the name of the first passenger is NOT the length of the array,
    One possible way to do is to pass another argument that indicates length.
    When passing the structure, do sizeof(rec) / sizeof(rec[0]).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    sizeof(rec) / sizeof(rec[0])
    will not work inn this case. rec is a pointer, so its size is 4 bytes on 32-bit system
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, but I assume it's declared on the stack before being passed to the function, in case it will work.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Elysia View Post
    Yes, but I assume it's declared on the stack before being passed to the function, in case it will work.
    Try it. It won't.

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by MacGyver View Post
    Try it. It won't.
    Maybe its just bad wording.

    It will work if we declare array of structs and then pass its size as shown.

    It will not work if we try to determine the array size in the function called
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Passing a structure to a function?
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2001, 04:28 AM