Thread: some questions about structure

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    96

    some questions about structure

    Hi,

    I have some questions about structure,wanted to know that when we make a struct using strings, do we have to make it outside of main() or inside it?

    I hope i am clear

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Normally when you define a structure you do it outside of any functions. However if the structure is only going to be used inside that particular function you can define it inside the structure.

    Jim

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I would not recommend structs. Instead, use a class with getters and setters. It will save you trouble when you want to modify how to set and get these variables later.
    No harm in being overcautious.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Elysia View Post
    I would not recommend structs. Instead, use a class with getters and setters. It will save you trouble when you want to modify how to set and get these variables later.
    I would only consider following that if programming in .NET. Otherwise just follow typical convention: Use a struct when it is a POD type and most things will be public.
    I've seen several comments lately about getters and setters, without any hint of a thought about the fact that such things indicate a lack of encapsulation, and so tend to be solving the wrong problem anyway. Also YAGNI ... so don't over-engineer it.

    student111: You can declare a struct wherever you like. Just be aware that if you declare it inside a function then you cant go any use it outside of that function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Over-engineering is bad. But is it truly such a waste of time to set up proper getters and setters? It takes not much more time, and thus, it can seen as a precaution.
    When you start accessing members of the struct at several places, it is typically time to set up getters and setters, but at that time, you've just screwed yourself over--now you must make (possibly) big changes to your code to make those getters and setters.
    As a precaution, whenever making changes to a struct, it may be worth changing about encapsulating such a thing to a function. This may be taking it to the extreme, but it will protect yourself from code changes later.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. some questions about structure
    By student111 in forum C++ Programming
    Replies: 4
    Last Post: 12-27-2011, 03:02 PM
  2. Questions on structure padding
    By sanddune008 in forum C Programming
    Replies: 1
    Last Post: 12-04-2011, 10:27 AM
  3. Replies: 4
    Last Post: 04-25-2010, 10:57 AM
  4. newbie: pointers to structure questions
    By cstudent in forum C Programming
    Replies: 11
    Last Post: 04-09-2008, 06:23 AM
  5. Passing a structure to a fxn questions
    By JackR in forum C++ Programming
    Replies: 9
    Last Post: 09-25-2006, 03:50 PM