Thread: set struct member variable for structure inside a structure def

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    27

    set struct member variable for structure inside a structure def

    This is with Linux gcc
    ---------------------------------------
    Code:
    typedef struct _a
    {
        int id;
    } a;
    
    typedef struct _b
    {
        a  my_a;
        my_a.id = 1;  // error: expected specifier-qualifier-list before my_a
    } b;
    I get error: expected specifier-qualifier-list before my_a

    I must set the id for the kind of struct created inside the struct def because
    main() will be casting based on this id. Thats how I will know which structure b contains by it's id, there could be hundards of different structs with different
    values I will cast to the correct one and know it's members by it's id.

    How do I ?


    .

  2. #2
    Registered User
    Join Date
    Mar 2014
    Posts
    14
    I dont know exactly what you are trying to achieve. But in C (not sure about C++), you cannot initialize a struct member whilst the defining of structure type is not complete. What this means is you cannot assign my_a.id a value untill you define a struct variable (b_t my_b in my example below). The compiler has not set aside any memory for the struct until you define it a varible (not a type).

    Code:
    typedef struct a{
        int id;
    } a_t;
    
    
    typedef struct b
    {
        a_t  my_a;
    } b_t;
    
    
    int main(void)
    {
        b_t my_b;  //definition of structure of type b_t with varible my_b;
        //From this line and onwards the compiler has set aside memory for the struct.
        my_b.my_a.id = 1;
    
        printf("Value of my_b.my_a.id: %d", my_b.my_a.id);
    
    
        return 0;
    }
    Last edited by Kotik; 03-12-2014 at 10:07 PM. Reason: Explained a little better.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    27
    Thanks so much, I understand now.

    *------------ Now, to the people who made this tool.

    Why did it not just say

    "Cannot assign values inside definitions, create a variable of the type first"

    Dont tell me that it's to specific. Thats only because the Boss told you it was and you were dumb enough to beleve them.

    I looked it up !

    *----------- And don't say thats why we created C++

    It's always easyer to start over than to do it right in the first place.

    I think we would be living on Mars by now if people really new what they were doing !

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,719
    Quote Originally Posted by Areal Person
    Why did it not just say

    "Cannot assign values inside definitions, create a variable of the type first"
    The compiler does not read your mind. Rather, the compiler parses the code that you write, and should it find that the code does not match the syntax that it expects according to the grammar of the language, it fails to compile, with an error message that best fits according to what was parsed.

    As such, sometimes the error message tells you exactly what is wrong, where and why. Other times, the error message merely tells you where the error was detected, but the code to fix lies elsewhere, or does not semantically match the hint provided by the error message.

    Quote Originally Posted by Areal Person
    Dont tell me that it's to specific. Thats only because the Boss told you it was and you were dumb enough to beleve them.

    I looked it up !
    You looked what up? Unless you have to actually know what you are talking about, I suggest that you refrain from making such statements.
    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
    May 2010
    Posts
    27
    @laserlight

    There is no scientific approval for C in it's current form. Only general acceptance as a utility tool.
    That why it say's very BOLDLY it has NO ... "FITNESS FOR A PARTICULAR PURPOSE." on the command line.

    Just do a cc --version on Linux. Thats what version "4.4.7" says. If they new it was correctly built, it would have scientific approval.
    and would be declared to be absolute". C is only accepted as a programming language. NOT a scientific solution.

    Did you make The C Compiler Standard, Were you on the Team who made the choices ? I looked at the Linux cc source code, and the ANSI compiler standards document last night.

    Don't be telling me to keep quite, you communist ! I'm in America, I'll say what I like, I wasen't talking to you anyway. You diden't Make the C compiler. (If you did, PROVE it and I'll apologize)

    All attributes can be classified logically and according to their type, and organized with descriptive information (at a reasonable logic level). Even a C Compiler, Just because the current Standard does not do that does not mean that It can not or should not be done.

    One of the first things the tool prints out and tells you that the people who made it have NO CONFIDENCE IN IT.

    It functions, nicly, and is a good tool. But it has always been lacking in the way it was constructed.
    Last edited by Areal Person; 03-13-2014 at 08:40 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,719
    Quote Originally Posted by Areal Person
    There is no scientific approval for C in it's current form. Only general acceptance as a utility tool.
    That why it say's very BOLDLY it has NO ... "FITNESS FOR A PARTICULAR PURPOSE." on the command line.
    You are confusing a legal disclaimer of a C compiler with a claim concerning what C is used for.

    Quote Originally Posted by Areal Person
    Just do a cc --version on Linux. Thats what version "4.4.7" says.
    cc is not C. It is a C compiler, or an alias thereof.

    Quote Originally Posted by Areal Person
    If they new it was correctly built, it would have scientific approval.
    and would be declared to be absolute".
    Utter rubbish. You have no clue as to what you are talking about.

    Quote Originally Posted by Areal Person
    C is only accepted as a programming language. NOT a scientific solution.
    Indeed. C is not a "scientific solution". So what? C is a programming language, and thus it can be used to write software solutions with scientific applications.

    Quote Originally Posted by Areal Person
    Did you make The C Compiler Standard, Were you on the Team who made the choices ?
    Nope, but that does not matter.

    Quote Originally Posted by Areal Person
    I looked at the Linux cc source code, and the ANSI compiler standards document last night.
    Did you understand them? If your aim is to impress me or intimidate me, I'm afraid that you have failed, since I can actually read and understand C code and have a level of understanding of the C standard that is unlikely for you to have acquired in a single night.

    Quote Originally Posted by Areal Person
    Don't be telling me to keep quite, you communist ! I'm in America, I'll say what I like, I wasen't talking to you anyway.
    You are mistaken. Refer to the forum guidelines. Whether or not you were addressing that statement to me, the fact that you posted in public here means that I am free to reply to it.

    Quote Originally Posted by Areal Person
    You diden't Make the C compiler. (If you did, PROVE it and I'll apologize)
    There are multiple C compilers available. I was not involved in writing any of them, but I do have some grasp of compiler construction, and it is from that store of knowledge that I answered you in post #4. Attacking a person who has freely shared knowledge with you is poor form.

    Quote Originally Posted by Areal Person
    All attributes can be classified logically and according to their type, and organized with descriptive information (at a reasonable logic level). Even a C Compiler, Just because the current Standard does not do that does not mean that It can not or should not be done.
    You can stop trolling now.

    If you are actually interested in understanding 'why did it not just say "Cannot assign values inside definitions, create a variable of the type first"', then refer to my explanation in post #4. Trying to talk trash about C just does not cut it.
    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
    May 2010
    Posts
    27

    Go figure

    Let’s talk about programming then.

    You’ve been totally brain washed. You’ve been programmed to believe this stuff your spewing.

    Anyone with an ounce of reasonable knowledge knows that the way societies operate, build tools, work together and problem solve etc. etc. are deeply flawed. Causing design issues and poor products.

    They just can’t do anything about it.

    If you think things like (science, politics tools, C compilers or etc.) are correct and right with the way things work, your living a lie.

    Some things may become scientifically valid, like algorithms, mathematics etc. But only after much trial and error, cost and your childrens children. (Standard C compiler specs. are badly flawed logicly)

    My God ! every scientist in the world got together at ITER and built the tokamak cassettes components out of some dumb stupid carbon mix the first time (wasting billions) when I would have known to use pure tungsten !

    https://www.iter.org/newsline/274/1639

    Dr. Doolittle, we should of had fusion energy 20 years ago.

    Ok, Dr. Doolittle. Let’s see how much you really do know anything.

    Your obviously so highly intelligent about logical things.

    Here’s a perfectly logical question.

    Why did the chicken cross the road?

    I promise… This one is not like the chicken and egg question.

    It’s not a trick question !
    It’s not a shotgun question. (That means legalistically ambiguous)

    Don’t be a jackass now.

    Go on, answer, If only in the spirit of justification for your obvious self-induced sense of Megalomania.

    My guess is that you’ll fall back on your communistic social programming and give some totally bogus answer as to why no one can give a correct answer.

    Well, that will validate my argument.

    Go figure.
    Last edited by Areal Person; 03-18-2014 at 08:17 AM.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Please stop posting such utter nonsense - it is contributing nothing to this forum. You need to learn how to make coherent points before trying to argue a position. I don't even think you know what point you're trying to make anymore.

    Don't be telling me to keep quite, you communist ! I'm in America, I'll say what I like, I wasen't talking to you anyway
    I'm also in America. Please stop making the rest of us look bad by spouting such blatant ignorance.

    And for the record, "you're" == "you are".

  9. #9
    Registered User
    Join Date
    May 2010
    Posts
    4,531
    Don’t be a jackass now.
    You've already got that covered. I really think you should go back on your meds, then maybe you can start thinking rationally.


    Jim

  10. #10
    Registered User
    Join Date
    May 2010
    Posts
    27
    I did not call anyone a jackass. You did. I said "Don't be one".

    Ua... You really need to look up things before you post

    -----------------------------------
    Jackass = stupid person

    stupid person =
    lacking intelligence or common sense.

    "I was stupid enough to think she (laserlight) was perfect"
    ------------------------------------

    @jimblumberg & Matticus

    All I did was as ask for the answer to a question.

    It's obvious, you are still mental children.

    ------------------------------------
    I don't take meds, I'm all natural.

    Just answer the question I asked.

    I started this thread.

    I made one comment about the error message design being poor in the C compiler. Which it is. Only to be treated ignorant and told to be quite.

    Don't blame me. I don't haft to shut up. And people who want to sensor by trying to tell people to be quite should have been quite there self to start with.

    @Matticus
    Just get out of this country, get out ! go somewhere you are REALLY wanted.
    Last edited by Areal Person; 03-18-2014 at 08:39 AM.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,719
    *thread closed*
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 09-04-2013, 10:09 AM
  2. assign string to the string variable inside structure!
    By king_zart in forum C++ Programming
    Replies: 20
    Last Post: 12-09-2012, 11:37 AM
  3. Replies: 3
    Last Post: 10-04-2012, 02:33 PM
  4. Problem with Structure inside a structure
    By purush12 in forum C Programming
    Replies: 11
    Last Post: 05-18-2011, 06:16 AM
  5. Defining structure inside another structure
    By mkdl750 in forum C Programming
    Replies: 1
    Last Post: 07-20-2008, 07:50 AM