Thread: Structures and setting variables

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    18

    Structures and setting variables

    Okay, I'm currently trying to make a program that uses structures. I've used the help document provided by the complier to give it a try and I know that I've done most of it right. The problem though is that it would appear as though I'm not setting a variable right because it gives the complier error: "expected constructor, destructor, or type conversion before '.' token " ... this is the line of code where the problem is occuring

    Code:
    standard.name = "Standard";
    It also give the error: "expected `,' or `;' before '.' token". I'm assuming that it's something that I'm doing wrong with this line because the lines before it defining the variables inside the structure don't cause any errors.

    Any suggestions?


    And by the way, anyone know how to link a library to a program without creating a project? (For Dev-C++)

  2. #2
    Registered User
    Join Date
    Aug 2007
    Posts
    25
    Can you post the code of your structure declaration and definition.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    How is standard.name defined? If it's a char[] array, you likely want to use strcpy() instead of ordinary assignment.

    How is standard defined? It should be an instance of a structure, but your compiler seems to think otherwise.

    And by the way, anyone know how to link a library to a program without creating a project? (For Dev-C++)
    No, not really. I mean you might be able to use a non-standard #pragma directive of some sort, but it would be a hassle.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    Code:
    struct ship
    {
           char name[20];
           int hull;
           int shields;
           int torp_str;
           int phaser_str;
    } standard, current, created;;
    
    
    standard.name = "Standard"; //error occurs on this line
    standard.hull = 100;        //this line
    standard.shields = 100;     //and this one
    standard.torp_str = 20;     //this one....
    standard.phaser_str = 40;   //and finally this one
    There's the code of the structure and the declarations. The error displayed is this:
    Code:
    expected constructor, destructor, or type conversion before '.' token
    and this
    Code:
    expected `,' or `;' before '.' token
    at the four lines that are commented.

    I don't understand though why strcpy() would work any better than just what I have for the character array since with non-structured variables you can store words in them in the same manner.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    It would seem to me that all your assignments are outside of a function.

    If you want to initialise your struct, do it like this.
    Code:
    struct ship standard = {
      "Standard",
      100,
      100,
      20,
      40
    };
    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.

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    Okay, I'll try that, but to change just a single variable, would I just use my previous code or would I have to do something else?

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That code creates a new struct named standard and initializes it to the given values. There's no such thing as a global structure initalization.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    Okay, thanks all. It's working now... now I have yet to figure out another problem

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by teck View Post
    Okay, thanks all. It's working now... now I have yet to figure out another problem
    Ain't that always the way [Just got that myself - I got past one point, only to find that the code deadlocks because an interface I'm calling is calling back into the module I'm using, which has locks to prevent it from being re-entered, so the whole thing just sits there waiting ... ]

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ahh, the wonders of deadlocks and faulty locking. Multi-threading issues are so fun, aren't they?
    Well, suppose it's life. No one can escape small mistakes and bugs. Find that bug, matsp, I'm sure you can, and squish it! Good luck.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Ahh, the wonders of deadlocks and faulty locking. Multi-threading issues are so fun, aren't they?
    Well, suppose it's life. No one can escape small mistakes and bugs. Find that bug, matsp, I'm sure you can, and squish it! Good luck.
    Yes, I know the solution (the code is attempting to initialize the module I'm calling from, which has already been initialized anyways, so we just need a "is this initialized" function), but in a large project you can't just go implement things in any file just willy-nilly - so I've worked around it for the moment by commenting the code out, until we have a proper function for asking if it's initialized. On to the next problem.

    Edit: and I prefer deadlocks from "strange things happen sometimes because two threads are trying to update the same data structure at the same time"....

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, I know what you mean. Been there, done that... infuriating, and usually hard to track down.

  13. #13
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    .... so I've worked around it for the moment by commenting the code out,....
    Yes the code I'm working on at work has many many green lines and blocks of green lines. I changed a lot but could not get it done in time to submit to source control. So I commented out all my new stuff and uncommented the old.

    I rarely erase anything until I know for sure I don't need it...and someone else doesn't need it.

  14. #14
    Registered User
    Join Date
    Nov 2007
    Posts
    18
    Lol, although, fortunately for me though, my problem seemed to be easier than yours

    But still, while we are off topic, does anyone have any ideas how one would (with Dev-C++) manually (as in, in the actual code) link a library?

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ehm, what do you "manually, as in the code"?

    You can add "-lsomename" to the linker configuarion, to link in "somename.lib", does that help?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. setting structure char array with string
    By Tom Bombadil in forum C Programming
    Replies: 6
    Last Post: 04-03-2009, 07:10 AM
  2. Setting a user's input as the structure name
    By Ste_Mulv in forum C Programming
    Replies: 3
    Last Post: 04-01-2009, 05:23 AM
  3. Array of records?
    By pobri19 in forum C Programming
    Replies: 6
    Last Post: 05-03-2008, 06:34 AM