Thread: struct question

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    86

    struct question

    Code:
    typedef struct
    {
    int a;
    }struct_ex  
    
    
    
    typedef struct example_1
    {
    
    union
    {
     struct_ex  object_ex
    }test1;
    
    
    }ex_1
    
    
    int main ()
    {
    
    ex_1 ex_obj;
    struct_ex   *z = &ex_obj->test1.object_ex;
    
    z->a = 1;
    
    }
    prog.cpp:13:1: error: expected initializer before 'typedef'
    typedef struct example_1
    ^

    error
    I am confused on how to pass values t o a object in a union within a struct? anyone can help

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    This is just invalid syntax because you forgot the terminating semi-colon:
    Code:
    typedef struct
    {
        int a;
    } struct_ex
    It is good that you posted your code within code bbcode tags, but you also need to indent your code properl.

    Quote Originally Posted by amahmoo
    I am confused on how to pass values to a object in a union within a struct?
    Why do you want to do this?
    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
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Code:
    #include <iostream>
    using namespace std;
    
    
    
    
    
    
    typedef struct
    {
    int a;
    }struct_ex;  
    
    
    
    
    
    
    typedef struct example_1
    {
    
    
        union
            {
          struct_ex  object_ex;
             }test1;
    
    
    
    
    }ex_1;
    
    
    
    
    int main ()
    {
    
    
    ex_1 ex_obj;
    struct_ex   *z = &ex_obj->test1.object_ex;
    
    
    z->a = 1;
    
    
    }
    
    new error
    prog.cpp: In function 'int main()':
    prog.cpp:29:25: error: base operand of '->' has non-pointer type 'ex_1 {aka example_1}'
     struct_ex   *z = &ex_obj->test1.object_ex;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    I suggest that you do not use a union because you probably don't need it, hence your reluctance to answer my question.
    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

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    I need the union there i need to do this for my hw for school laserlight can you help?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Quote Originally Posted by amahmoo
    I need the union there i need to do this for my hw for school
    Besides this being homework, why do you need the union?

    Quote Originally Posted by amahmoo
    laserlight can you help?
    No, because I cannot make head or tail what you are trying to do. You're basically dumping syntactically invalid code, or semantically suspect code, and then expecting that someone will figure it out for you.

    Rather, you should start with an overview of what you are trying to do. What is the problem that you are writing code to solve?

    Then, show the code. The code should be properly formatted, especially where indentation is concerned. It is usually a good idea to post the smallest and simplest (compilable) program that demonstrates the error, if not at least post a self-contained snippet, e.g., a function that does not rely on global variables and other declarations that are not shown.

    Finally, explain how does your code not work. Is it a compile error? If so, what is the exact error message? Is it a logic error? If so, what is the test input, expected output, and actual output? Does your program crash? If so, run it through a debugger and pinpoint on which line did the crash happen: that can help narrow down where the mistake might be, and of course you would provide details about test input, etc.
    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

  7. #7
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    i am just trying to set a to 1 using the following code up there its from my hw he told us to do it this way it doesnt matter what you think is semantically correct he just doing it to think differently

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Quote Originally Posted by amahmoo
    i am just trying to set a to 1 using the following code up there its from my hw he told us to do it this way it doesnt matter what you think is semantically correct he just doing it to think differently
    What exactly did your instructor say? You're basically saying "my instructor told me to do this, but it doesn't work". When I ask you what you're trying to do, you reply "what my instructor told me". You might as well ask your instructor for help because no one else has the necessary information to help you. We could guess, but if you really did what your instructor told you and refuse to accept anything else, for all we know the mistake lies with your instructor.

    By the way, are you programming in C or C++?

    EDIT:
    Quote Originally Posted by amahmoo
    it doesnt matter what you think is semantically correct he just doing it to think differently
    If you're saying that what I think doesn't matter, why should I bother helping you? My thoughts don't matter, right? I'm not relevant, a non-entity. Go ask your instructor for help. You're paying him, not me.
    Last edited by laserlight; 12-22-2015 at 10:05 AM.
    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

  9. #9
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Quote Originally Posted by laserlight View Post
    What exactly did your instructor say? You're basically saying "my instructor told me to do this, but it doesn't work". When I ask you what you're trying to do, you reply "what my instructor told me". You might as well ask your instructor for help because no one else has the necessary information to help you. We could guess, but if you really did what your instructor told you and refuse to accept anything else, for all we know the mistake lies with your instructor.

    By the way, are you programming in C or C++?

    EDIT:

    If you're saying that what I think doesn't matter, why should I bother helping you? My thoughts don't matter, right? I'm not relevant, a non-entity. Go ask your instructor for help. You're paying him, not me.
    look i didn't mean what you think doesn't matter. What you say does matter. I am sorry i didn't mean to sound that way also its my fault i am not clear in what i am asking is becase i dont understand what i am supposed to do. My instructor is a piece of ........ that just hands out code and says do it this way and does not offer any help.
    I get frustrated and i am desperate for any help. I appreciate your help alot you have no idea the questions u ask and guidance you give helps me alot i really do appreciate it

  10. #10
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Quote Originally Posted by laserlight View Post
    What exactly did your instructor say? You're basically saying "my instructor told me to do this, but it doesn't work". When I ask you what you're trying to do, you reply "what my instructor told me". You might as well ask your instructor for help because no one else has the necessary information to help you. We could guess, but if you really did what your instructor told you and refuse to accept anything else, for all we know the mistake lies with your instructor.

    By the way, are you programming in C or C++?
    I am programming in c

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,403
    Quote Originally Posted by amahmoo
    I am programming in c
    Right. Did your instructor really tell you to do:
    Code:
    #include <iostream>
    using namespace std;
    We may have told you before, but that's a C++ header and the using directive is C++ syntax.

    Quote Originally Posted by amahmoo
    look i didn't mean what you think doesn't matter. What you say does matter. I am sorry i didn't mean to sound that way also its my fault i am not clear in what i am asking is becase i dont understand what i am supposed to do.
    Fair enough. Well, start by indenting your code properly:
    Code:
    #include <iostream>
    
    using namespace std;
    
    typedef struct
    {
        int a;
    } struct_ex;
    
    typedef struct example_1
    {
        union
        {
            struct_ex object_ex;
        } test1;
    } ex_1;
    
    int main()
    {
        ex_1 ex_obj;
        struct_ex *z = &ex_obj->test1.object_ex;
        z->a = 1;
    }
    Now, it is easy to see that ex_obj is an ex_1 object. Therefore, it has a member named test1, which you should access as ex_obj.test1. ex_obj.test1 is a union with only one member, named object_ex. Therefore, you should access this member as ex_obj.test1.object_ex. So, if you want a pointer to this object:
    Code:
    struct_ex *z = &ex_obj.test1.object_ex;
    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

  12. #12
    Registered User
    Join Date
    Nov 2015
    Posts
    86
    Quote Originally Posted by laserlight View Post
    Right. Did your instructor really tell you to do:
    Code:
    #include <iostream>
    using namespace std;
    We may have told you before, but that's a C++ header and the using directive is C++ syntax.


    Fair enough. Well, start by indenting your code properly:
    Code:
    #include <iostream>
    
    using namespace std;
    
    typedef struct
    {
        int a;
    } struct_ex;
    
    typedef struct example_1
    {
        union
        {
            struct_ex object_ex;
        } test1;
    } ex_1;
    
    int main()
    {
        ex_1 ex_obj;
        struct_ex *z = &ex_obj->test1.object_ex;
        z->a = 1;
    }
    Now, it is easy to see that ex_obj is an ex_1 object. Therefore, it has a member named test1, which you should access as ex_obj.test1. ex_obj.test1 is a union with only one member, named object_ex. Therefore, you should access this member as ex_obj.test1.object_ex. So, if you want a pointer to this object:
    Code:
    struct_ex *z = &ex_obj.test1.object_ex;


    thank you this is what i needed to know i appreciate it so much love you laserlight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. struct question
    By ranjit89 in forum C Programming
    Replies: 3
    Last Post: 11-01-2011, 03:59 AM
  2. Replies: 1
    Last Post: 03-23-2011, 09:00 AM
  3. Struct delcaring a struct within itself question
    By illidari in forum C Programming
    Replies: 3
    Last Post: 12-03-2010, 03:01 PM
  4. Struct question
    By 182 in forum C++ Programming
    Replies: 7
    Last Post: 02-21-2006, 02:55 PM
  5. Question about list (struct {..struct *next}
    By jmzl666 in forum C Programming
    Replies: 1
    Last Post: 10-23-2002, 01:34 AM