Thread: Help With Structures

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

    Help With Structures

    Hello I wrote this code but it's not working. I would be very greatful if anyone could help me out. It passes the structure as a function parameter into another function and returens it's values for the desired variable. I will be very graet full if anyone could help me and point out the mistakes.

    Code:
    #include <stdio.h>
    
    float test(information_t in);
    
    typedef struct{
    	int number;
    	int place;
    	int postcode;
    } information_t;
    
    int main()
    {
    	information_t information;
    	information *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(information_t in);
    
    return 0;
    }
    
    float (information_t in)
    {
    	char position;
    	position= scanf(" %d%d%d", in->number, in->place, in->postcode);
    	
    return(position);
    }

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Few things:

    1. You use information_t before declaring it, in your function prototype for test.
    2. When you define the test function, you haven't given it a name, you have float (information_t in) instead of float test (information_t in)
    3. When you call the test function, you are passing the type, when you shouldn't be, just pass the parameter.
    4. I think your intention was to pass a pointer to the information_t struct, not the value, so you need float test (information_t *in) and call with test(&in);
    5. The scanf's in main are correctly done with an & to get the address of the field, but the scanf in the test function has just in->number, when it should be &in->number
    6. Your function is declared to return a value, but you do nothing with the returned value.
    7. scanf returns an int, but you pass a char.
    8. Your function returns a float, but you are returning an integer type.
    Last edited by cwr; 09-23-2005 at 02:33 AM.

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

    Help with Structures

    Thanks cwr for your advice it really did help clear up a few things.

    But I just don'nt know how to pass the parameter to the gunction test. I want to pass the sturcture as a function parameter and uses it's variables in the new function. I was hoping If somebady can show me how to do it.

    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 *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(&information_t  *in);
    
    return 0;
    }
    
    int test(information_t  *in)
    {
    	int position;
    	position= scanf(" %d%d%d", &in->number, &in->place, &in->postcode);
    	
    return(position);
    }

  4. #4
    Registered User
    Join Date
    Sep 2005
    Posts
    20
    Quote Originally Posted by HAssan
    Code:
    	information_t information;
    	information *in;
    what do you think this section of code is doing? information is a variable of type struct. You cannot create a pointer to a variable in this manner. You could, however do something like this:
    Code:
    information_t information;
    information_t * in = &information;
    which will create a pointer to the variable information of type "information_t". I think this is what you are trying to accomplish (just guessing though).

    Code:
    test(&information_t  *in);
    ABSOLUTELY WRONG.
    Your function test takes a variable of type pointer-to-information_t. You are passing it the address of a structure (which i'm not sure if this even exists) and then a dereference of a pointer-to-(variable of type information_t), which is of course not valid at all.

    You need to have something like this
    Code:
    some_int_variable = test(in);
    or more simply without the pointer declaration earlier
    Code:
    some_int_variable = test(&information);
    which of course assumes that some_int_variable is declared earlier as an int.

    Which leads me to my next point: Why does your test function return an int? It serves no purpose. Why not just use void return type?

  5. #5
    People Love Me
    Join Date
    Jan 2003
    Posts
    412
    Code:
    	information_t information;
    	information *in;
    "information" does not name a type. That won't work at all.

    Code:
    information_t* in;
    By the way, that creates a "wild" pointer, one's that's not pointing to anything. Either give it some memory with malloc() or assign it to 0 or NULL.
    Last edited by Krak; 09-24-2005 at 03:34 AM.

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