Thread: Trouble Passing Structures

  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
    28,413
    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.
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    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

  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
    28,413
    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.
    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

  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, 11: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