Thread: Structs ,pointers and functions - need some help

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    58

    Question Structs ,pointers and functions - need some help

    Hi,
    could you please help me?
    I have a struct and I want to implement in with pointers and functions.
    Q - what is the corect syntax?

    for example:

    Code:
    typedef struct XYZ
    {
    
        int x;
        int y;
        int z;
       }XYZ_t;
    
    
    int func(  using the XYZ_t struct)
    {
      
    }
    
    int main()
    {
        
        XYZ_t *XYZ_t_PTR;
        func(  using the XYZ_t struct);
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    That question is as unclear as mud.

    Your description of wanting to "have a struct" that is implemented "in with pointers and functions" is meaningless.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Which book are you reading?

    Because it isn't any good.

    Let me put it this way.

    If you had
    int *XYZ_t_PTR;

    would you still be as confused as you are now?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    I have a struct and I want to implement in with pointers and functions.
    I don't think anyone can help you with that unless you become more specific about what "in" is.

    Q - what is the corect syntax?

    for example:

    Whatever that example is, it isn't valid C. I think you would benefit from learning the difference between objects and types.

    I'm wondering, do you have a reason to pretend that XYZ_t's underlying type isn't struct XYZ?

  5. #5
    Registered User
    Join Date
    Feb 2013
    Location
    Espoo, Finland
    Posts
    1
    I think you mean something like this:

    Code:
    typedef struct XYZ
    Code:
    {
    
        int x;
        int y;
        int z;
       }XYZ_t;
    
    
    int func(XYZ_t *X) /* a pointer to struct */
    {
     X->x = 1;
    }
    
    int main()
    {
    
        XYZ_t *XYZ_t_PTR; /a pointer to struct */
    XYZ_t structXYZ; /The struct */
    XYZ_t_PTR = &structXYZ;
    
        func(XYZ_t_PTR);
    }
    


    Last edited by Ilkka Mannelin; 02-23-2013 at 03:15 AM.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Whhhhhhyyyy does everyone get taught to use typedef? It's a convenience to not have to type 'struct' all the time. Just a convenience.

    When I first encounted a C struct I was told to use typedef as well, and it confused the hell out of me (because it looks a bit like an anonymous definition, with this mysterious typedef keyword that I don't fully understand). Just seems like the two concepts always get bundled together too early for most people.

    Anyway. Mini rant over.

    Code:
    typedef struct XYZ
    { 
        int x;
        int y;
        int z;
       }XYZ_t;
     
    int func(XYZ_t *s)
    {   
    }
    
    int main()
    {     
        XYZ_t *XYZ_t_PTR;
        func(XYZ_t_PTR);
    }
    Is what I think you're asking. XYZ_t is a type name though not an instance of a struct. Your pointer points to nothing -- uninitialised garbage.

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. Array of Pointers to Functions in Structs...
    By yaya in forum C++ Programming
    Replies: 10
    Last Post: 12-21-2008, 06:14 PM
  3. passing pointers at structs thru functions
    By nunnu in forum C Programming
    Replies: 2
    Last Post: 05-12-2004, 09:16 AM
  4. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM
  5. Pointers, structs, and functions, oh my!
    By funkydude9 in forum C++ Programming
    Replies: 8
    Last Post: 07-30-2003, 12:51 AM