CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 3 of 3 FirstFirst 123
Results 31 to 36 of 36
  1. #31
    Join Date
    Feb 2012
    Location
    MD
    Posts
    44

    Re: My very first C program (Windows - console)

    Quote Originally Posted by Paul McKenzie View Post
    In practically every single mid to high level language, there are such things as local and global variables. Whether it is C, C++, Pascal, FORTRAN, VB, C#, Java, I could go on and on listing all the languages where local and global variables exist.

    In none of these languages is it desirable to "globalize" your variables to such an extent as your code does. It isn't just the few voices here saying it, every expert will tell you the same thing.

    Regards,

    Paul McKenzie
    I'm well aware that many languages have local/global variables. I've never ever had to address this concept until a month ago.

    What should I do when many of these arrays are accessed in multiple functions?

  2. #32
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: My very first C program (Windows - console)

    Quote Originally Posted by jlewand61
    What should I do when many of these arrays are accessed in multiple functions?
    The usual approach is to pass to a function only what it needs to do its work, so you would pass a pointer to the first element of the array as an argument when calling a function that accesses the elements of the array. The array itself would be local to a function, e.g., the main function. It may be the case that say, two arrays are logically part of some kind of object, in which case you might define a struct containing these two arrays, then pass a pointer to the struct instead.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #33
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: My very first C program (Windows - console)

    Quote Originally Posted by jlewand61 View Post
    It shouldn't bother anyone whether I take advice or not. It wouldn't bother me if someone deflected my advice. I've sincerely asked to help me get into good C coding habits. C is a new animal and I'm trying to develop good habits from the start.
    It bothers us because we are taking the time to review your code, and tell you what we think about it, and how to make it better. Yet you seem bent on ignoring what is said, hence our time is wasted...

    ----
    That said, if you want my advice about trying to move from assembler to C, then I'd actually suggest you learn C++ !

    For you, C may only feel like an evolution from assembly, making it hard to shake your old habits, as it allows you to do things "the good old assembly way"

    C++ won't take that crap. If you take the time to learn some if it correctly, chances are it will shift your mindset, making it more prepared to learn the C way. C++ will teach you proper encapsulation, abstraction etc.

    Once you've learned those, you'll realize they can also be used in C, and will strive to use them.

    That and learning C++ is also useful. IMO learning C => C++: bad idea. C++ => C works perfectly fine.

    My 0.02$
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #34
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: My very first C program (Windows - console)

    Quote Originally Posted by jlewand61 View Post
    I don't see any point in breaking a function into smaller pieces just because it's too long.
    One of the quotes I used to have pinned over my desk.

    "There are two ways of constructing a software design. One way is to make it so simple that there are obviously no deficiencies. And the other way is to make it so complicated that there are no obvious deficiencies."
    C.A.R. Hoare
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  5. #35
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: My very first C program (Windows - console)

    Quote Originally Posted by monarch_dodra View Post
    It bothers us because we are taking the time to review your code, and tell you what we think about it, and how to make it better. Yet you seem bent on ignoring what is said, hence our time is wasted...

    ----
    That said, if you want my advice about trying to move from assembler to C, then I'd actually suggest you learn C++ !

    For you, C may only feel like an evolution from assembly, making it hard to shake your old habits, as it allows you to do things "the good old assembly way"

    C++ won't take that crap. If you take the time to learn some if it correctly, chances are it will shift your mindset, making it more prepared to learn the C way. C++ will teach you proper encapsulation, abstraction etc.

    Once you've learned those, you'll realize they can also be used in C, and will strive to use them.

    That and learning C++ is also useful. IMO learning C => C++: bad idea. C++ => C works perfectly fine.

    My 0.02$
    Yeah. If you're going to forget everything you know and start over, it's best to only do it once.

  6. #36
    Join Date
    Apr 1999
    Posts
    27,449

    Re: My very first C program (Windows - console)

    Quote Originally Posted by jlewand61 View Post
    I'm well aware that many languages have local/global variables. I've never ever had to address this concept until a month ago.
    Well, if you think that's a big step, wait till you get to programming multithreaded applications. In that paradigm, excessive globalizing of variables is a death blow.

    I also agree that if your real goal is to learn C++ (and not actually just 'C'), then you need to learn C++. If you are learning 'C' to prepare yourself to learn C++, well, don't. Go directly to C++.

    Regards,

    Paul McKenzie

Page 3 of 3 FirstFirst 123

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured