From: rlcarr@animato.network23.com (Rich Carreiro)
Newsgroups: comp.sys.amiga.programmer,alt.folklore.computers
Followup-To: comp.sys.amiga.programmer
Subject: Re: How should I learn C?
Distribution: world
X-NewsSoftware: GRn 1.16f (10.17.92) by Mike Schwartz & Michael B. Smith
Date: 29 Dec 92 00:20:32 EST
Organization: The Other Side of Life

In article <78849@hydra.gatech.EDU> gt6758b@prism.gatech.EDU (Michael Maverick Kopack) writes:
> There are a lot of things about C that I find very frustrating after comming
> from a decent course in Pascal. For one is the way that Arrays are handled.
> I like being able to do stuff like    array [6..26] of int whereas in C you
> get forced into calling the indeces 0-19. I also HATE that there are no true 
> pass by reference parameters! 

[note: array [6..26] of int has 21 elements, and therefore would have indices
0-20 in C, not 0-19]

Yes there are -- you pass in a pointer.  This is NO different than what 
Pascal does --- Pascal simply uses "syntactic sugar" to hide it from you, but
passing a pointer is what it is actually doing in this case.  As for arrays
that start at other than 0, once you understand pointers there's a relatively
easy way, but use at your own risk:
RANDOMTYPE foo_bad[21];
RANDOMTYPE *foo;
foo = &foo_bad[0] - 6;

You now can use foo[6] to foo[26] to access the arrays.  By extension you
can even use negative subscripts (you can in theory -- I don't know off-hand
if the language spec allows for it).

> 
> It seems that C pretty much took Pascal and threw all the rules out the window!

Isn't C older than Pascal?  In any case, get a copy of Brian Kernighan's classic
_Why Pascal Is Not My Favorite Programming Language_ (or some similar title).
It's available somewhere; it was posted to alt.folklore.computers a month or
so ago.  It's a wonderful point-by-point listing of the serious things that
are wrong with Pascal.

> I can't imbed functions so other procedures can't see them (scoping rules),

True -- this can be annoying.  However, you can minimise namespace collisions
somewhat by the judicious use of the "static" keyword.  Let's say that
functions foo() and bar() are supposed to be visible to the world, and that
auxfoo() and auxbar() are functions that foo() and bar() need buy shouldn't
be world visible.  Create a sourcefile (say foobar.c) and define the functions
as follows:

RANDOMTYPE foo(whatever)
{
 /* foo's code */
}
RANDOMTYPE bar(whatever)
{
 /* bar's code */
}
static RANDOMTYPE auxfoo(whatever)
{
 /* auxfoo's code */
}
static RANDOMTYPE auxbar(whatever)
{
 /* auxbar's code */
}

Again, this is all within a single sourcefile, distinct from the other
sourcefile which make up the rest of the program.  If you do this, only
foo() and bar() will be visible to functions contained in other sourcefiles,
while auxfoo() and auxbar() will be visible to foo(), bar(), and each other.

> I have to use global variables a lot (taught as a no-no at GT) and it just

There's nothing in C that should make you use globals where you did not use
them in Pascal.  I don't understand this complaint.

> seems like C is TOO flexible! How do you guys get anything done when there are
> about 500 different ways to do it?

There are ways that are generally objectively better (though of course the
particulars of a program might override "accepted" ways).  Use them.  I know
that's not much of an answer :-)  Check out some books on C,
starting with the 2nd edition of Kernighan and Ritchie.  I've also liked
_C Primer Plus_ by the Waite group.  I'm sure others will have recommendations.
In any case, I'd rather have many ways to do something than have no way (which
can happen in Pascal too often).

> Please, open my mind to C! I'd hate to have to try to do everything for my 
> Amiga in Pascal, considering how much of the Amiga software is written in 
> and documented for C......

Well -- this was my crack at it... :-)

> Michael Maverick Kopack                | Sex, drugs, rock and roll  
> Internet: gt6758b@prism.gatech.edu     |and my Amiga, what else could
>           kopack@vortex.erda.rl.af.mil |one ever want?     
>           kopackm@lonex.rl.af.mil      \---------------------------------------

--
Rich Carreiro                                  Home: (401)841-8514
rlcarr@animato.network23.com
uunet.uu.net!animato!rlcarr
Newsgroups: comp.sys.amiga.programmer,comp.sys.amiga.hardware