Thread: Problem with structures

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Problem with structures

    I wrote this program but it does not return the information i want it excutes fine but it does not return what i want it to.

    Code:
    #include <stdio.h>
    
    typedef struct{
    	int number;
    	int place;
    	int postcode;
    } information_t;
    
    int test(information_t *in);
    
    int main()
    {
    	information_t information;
    	information_t *in; 
    	
    printf(" Please enter the number: ");
    scanf(" %d",&information.number);
    
    printf(" Please enter the place: ");
    scanf(" %d",&information.place);
    
    printf(" please enter the postcode: ");
    scanf(" %d",&information.postcode);
    
    test(in); 
    
    return 0;
    }
    
    int test(information_t  *in)
    {
    	int position;
    	position= printf(" The number is %d the place is %d and the postcode is %d\n", &in->number, &in->place, &in->postcode); 
    	
    //printf(" The number is %d the place is %d and the postcode is %d\n", &in->number, &in->place, &in->postcode); 
    	
    return(position);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    So how about actually asking a smart question? What do you expect it to do? What does it do that it shouldn't? What doesn't it do that it should? Sample input / output?


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    92

    Details

    the code is meant to ask the user for 3 inputs number,place(in form of numbers) and a postcode

    here is what it should look like

    Please enter the number: 10
    Please enter the place: 10
    please enter the postcode: 10
    The number is 10 the place is 10 and the postcode is 10

    but here is what it looks like

    Please enter the number: 10
    Please enter the place: 10
    please enter the postcode: 10
    The number is 1324234 the place is 1324238 and the postcode is 1324242
    Press any key to continue...

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    You are passing a pointer (in) to the test function, you haven't done anything with this pointer, it's uninitialised.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The "other" problem though, is that you're printing the address of all your variables, not their values.
    Lose the &.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    That too, I didn't even look inside the function.

    That would explain the suprising lack of crash from the original code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. searching problem
    By DaMenge in forum C Programming
    Replies: 9
    Last Post: 09-12-2005, 01:04 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM