Trouble Passing Structures

This is a discussion on Trouble Passing Structures within the C Programming forums, part of the General Programming Boards category; I am trying to pass structures that I have in an array. The code compiles but the programs spews trash ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    22

    Trouble Passing Structures

    I am trying to pass structures that I have in an array. The code compiles but the programs spews trash when it runs. Does anybody have an suggestions for me to fix this?

    Code:
    #include <stdio.h>
    #include <string.h>
    
                    
    typedef struct {
            
            char name[20];
            int number;
    } id;
     
    void input (id, int);
    
    void read (id);
     
    int main (void)
            
    {
            
            id array[2];
            
            int index;
            
            for(index = 0 ; index < 2 ; index++)
            
                    input(array[index], index);
            
            printf("Here are the two names with their ID:\n\n");
    
            for(index = 0 ; index < 2 ; index++)
    
                    read(array[index]);
            
            printf("\n\n");
            
            return(0);
            
    }
     
            
    void input (id current_struct, int number)
            
    {       
            
            printf("\n\nPlease enter name %d: ", number+1);
            
            scanf("%s", current_struct.name);
            
            printf("Please enter %s's id number: ", current_struct.name);
            
            scanf("%d", &current_struct.number);
            
            return;
    
    }
    
            
    void read (id reading_struct)
                    
    {       
            
            printf("Name:%s \nID:%d",reading_struct.name,reading_struct.number);
            
            return;
     
    }
    Thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    Pass an id pointer instead of an id to those functions (not necessary for read(), which really should be named write() or print(), but you don't need a copy anyway). If not, your input() function merely modifies its local copy of the id struct.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    you passing struct by value - so its is copied into the local var... changes made inside the function do not reflect in the original instanse.

    your original instanse is not initialized...

    use
    Code:
    id array[2] = {0};
    to zero initialize the array, and pass your structs by pointer - to update the original variables and not copies
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    22
    Thanks for the insight so far! I initialized the array, but...

    I am not sure how to make my structure a pointer. I tried this, but it is not right:

    Code:
    ....
    
    void input (id *, int);       //my prototype
    
    ....
    
    input(*array[index], index);        //my call
    
    ....
    
    void input (id *current_struct, int number)
            
    {       
            
            printf("\n\nPlease enter name &#37;d: ", number+1);
            
            scanf("%s", current_struct.name);        //ERROR
            
            printf("Please enter %s's id number: ", current_struct.name);  //ERROR
            
            scanf("%d", *&current_struct.number);     //ERROR
            
            return;
    
    }
    I recall something about strings not needing to use a pointer, but I am probably wrong. I tried compiling like above and with pointers for my strings.

    I get this error three times referring to the three lines above:

    request for member 'name' in something not a structure or union
    'number'

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,968
    This:
    Code:
    input(*array[index], index);
    should be:
    Code:
    input(&array[index], index);
    Now, if current_struct is a pointer, you would access the name member by writing current_struct->name instead of current_struct.name. The same goes for the other parts that involve the change to a pointer.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    22
    Thank you so much! That worked for me!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing An Array Of Structures To A Function
    By nexusdarkblue in forum C Programming
    Replies: 3
    Last Post: 03-10-2009, 07:24 AM
  2. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 10:01 AM
  3. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM
  4. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 11:22 AM
  5. passing array of structures to function
    By bvnorth in forum C Programming
    Replies: 3
    Last Post: 08-22-2003, 07:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21