Thread: probs with structs and functions

  1. #1
    Unregistered
    Guest

    Unhappy probs with structs and functions

    I've been asked to create a "file" system if you will that needs to incorporate nested structs. I have a problem passing my structs to my functions.

    EX:
    struct Disc
    {
    char arrayofalbums[30];
    int numberofalbums;
    int totalplayingtime;
    } disc[10];

    then later on I pass it into a function

    file_function(disc);


    I get an error saying how my var (disc) cannot be converted from a structure to char. Am I doing something wrong? I'm not trying to pass in the individual fields as arguments but the whole structure.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Can you post the functions prototype and source. (Dont forget the code tags please!)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Re: probs with structs and functions

    Originally posted by Unregistered
    I've been asked to create a "file" system if you will that needs to incorporate nested structs. I have a problem passing my structs to my functions.

    EX:
    struct Disc
    {
    char arrayofalbums[30];
    int numberofalbums;
    int totalplayingtime;
    } disc[10];

    then later on I pass it into a function

    file_function(disc);


    I get an error saying how my var (disc) cannot be converted from a structure to char. Am I doing something wrong? I'm not trying to pass in the individual fields as arguments but the whole structure.
    Does your function look like this?

    void file_function(char whatever);
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Unregistered
    Guest

    Unhappy

    Well this is what I have so far of my code:

    //////////////////////////////////////////
    #include <stdio.h>
    #include <string.h>

    void read_album(char *disc)
    {


    }



    int main()
    {


    struct Disc
    {
    char arrayofalbums[30];
    int numberofalbums;
    int totalplayingtime;
    } disc[10];


    read_album(disc);


    return 0;
    }
    //////////////////////////////////////////

    I know it doesn't do anything right now, I'm just trying to figure out how to pass the structure into the function. If this isn't possible, is there some other alternative other than using global structures?

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, it really depends on how it's all going to hang together, but if all you want to do is pass the struct to a function, examine this example prog:
    Code:
    #include <stdio.h>
    
    struct mystruct
    {
    	int num1;
    	int num2;
    };
    
    void ShowStruct(struct mystruct);
    
    void ShowStruct(struct mystruct myS)
    {
    	printf("%d %d\n", myS.num1, myS.num2);
    }
    
    int main(void)
    {
    	struct mystruct s;
    
    	s.num1 = 10;
    	s.num2 = 20;
    	
    	ShowStruct(s);
    		
    	return(0);
    }
    This shows a struct being passed from main() to ShowStruct(). Does that help you understand better?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Unregistered
    Guest

    Unhappy

    Yes, I understand your example and see how it works. However, this program uses a global structure. My problem requires that I use a local structure. Is there anything you can show me that will help me do this?

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Try this

    Code:
    typedef struct
    { 
    char              arrayofalbums[30]; 
    int               numberofalbums; 
    int               totalplayingtime; 
    } DISC;
    
    void read_album(DISC*   localdisc) 
    { 
    
    } 
    int main() 
    { 
    DISC        disc[10];
    
    read_album(&disc[0]); 
    }
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >My problem requires that I use a local structure.
    You won't be able to pass it to another function then, not without some trickery.

    You need to define the structure before the function is called so that you can have it as a parameter to that function. The only other way I can see doing it, is to use void pointers as parameters, and have the struct defined in both the functions. But what's the point?

    I'm sure you need to do it the way I and novacain have showed you. Both bits of example code handle the passing differently, one passes the struct, the other passes a pointer to an array of structs. And both use globally defined structs.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    *
    Guest
    Nested structs pass fine. The reason you are getting the error is because you are trying to pass the entire struct on the stack. You need to pass it by address.

  10. #10
    Unregistered
    Guest

    Unhappy

    Could you maybe show me an example of what you are talking about? I don't quite understand.

  11. #11
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>>My problem requires that I use a local structure.

    A local INSTANCE (as the examples provide) or DECLARATION of the structure?

    (an instance is the actual variable of type BLAH as opposed to the declaration where you define the elements in the struct BLAH.)

    I think this is homework. So I think you are expected to show you can pass a struct (or large variable/array) around between functions. This is better/has to be done by adress. You can use void pointers and type casts and multiple struct declarations to get a local declaration (multiple struct declarations are not used in the real world as they lead to very bad errors).

    >>Could you maybe show me an example of what you are talking about? I don't quite understand.

    Look at the code I wrote. This passes the address of the instance of the struct.

    int iInt=0;

    int MyFunct(int *iIndex);//prototype

    MyFunct(&iInt);//call to send address of int var

    >>However, this program uses a global structure.
    No. The struct is local to the main function.

    It uses a local struct but is not passed by address which will lead to errors if the struct is large.
    Last edited by novacain; 05-08-2002 at 11:55 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs, pointers and functions
    By osici in forum C Programming
    Replies: 2
    Last Post: 04-29-2009, 12:35 AM
  2. Passing structs to callback functions (GTK+ related)
    By Raskalnikov in forum C Programming
    Replies: 2
    Last Post: 03-21-2009, 12:46 PM
  3. pointer probs. with structs.
    By dinjas in forum C Programming
    Replies: 6
    Last Post: 03-08-2005, 05:59 PM
  4. Pointers, structs, and functions, oh my!
    By funkydude9 in forum C++ Programming
    Replies: 8
    Last Post: 07-30-2003, 12:51 AM
  5. passing structs to functions?
    By Neildadon in forum C++ Programming
    Replies: 1
    Last Post: 12-13-2002, 04:31 PM