|
-
February 28th, 2012, 09:17 PM
#31
Re: My very first C program (Windows - console)
 Originally Posted by Paul McKenzie
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?
-
February 28th, 2012, 09:27 PM
#32
Re: My very first C program (Windows - console)
 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.
-
February 29th, 2012, 05:29 AM
#33
Re: My very first C program (Windows - console)
 Originally Posted by jlewand61
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.
-
February 29th, 2012, 06:28 AM
#34
Re: My very first C program (Windows - console)
 Originally Posted by jlewand61
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
-
February 29th, 2012, 08:22 AM
#35
Re: My very first C program (Windows - console)
 Originally Posted by monarch_dodra
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.
-
February 29th, 2012, 09:25 AM
#36
Re: My very first C program (Windows - console)
 Originally Posted by jlewand61
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|