[comp.sys.atari.st] Laser C function call help needed!

n8742883@unicorn.WWU.EDU (Perry Pederson) (07/12/90)

I have just purchased Laser C, and after a week of book hunting was
able to purchase a copy of "The C Programming Language", second
edition, by B. Kernighan and D. Ritchie.  I was advised to buy this
book as I was told it was the "bible" for C.  I was learning a lot of
the basic functions of C and having a pretty good time learning the
language until I hit the section on functions.

	It appears to me that Laser C is _not_ ANSI compatible, which
really burns me.  I got massive syntax errors when I tried to compile
a simple function.  The next page (p.26) the authors showed a "note of
history" where "The biggest change between ANSI C and earlier versions
is how functions are declared and defined...." and gave an example of 
a function "would have been written" in the older versions of C.  I
wrote the old version in and it compiled and ran perfectly.

	Satisfied that I could overcome a small syntactical problem
with the language, I went off to the next example which has an array
passed to a function, and the function returns an integer value.  The
compiler hated it. I have tried for an hour to juggle the syntax
around, but it still won't compile at all.  The algorithm is as
follows as per the book:

/* getline: read a line into s, return length */

int getline(char s[], int lim)
{
	int c,i;

	for (i=0; i<lim-1 && (c = getchar())!=EOF && c!='\n'; ++i)
		s[i] = c;
	if (c == '\n') {
		s[i] = c;
		++i;
	}
	s[i] = '\O';
	return i;
}

	The Laser C compiler screams bloody murder on the first line;
It insists that there is a missing ')', missing ';', and then declares
that there is an invalid function.  I have tried to 'mimic' their
example of 'older' versions of C programming by declaring the
variables under the function declaration and removed the beginning
'int', thus reading:


/* getline; read a line into s, return length */

getline(s, lim)
char s[], int lim;
{
  etc. etc. etc. 
}


	but the compiler still tells me that the function is declared
wrong, and that the 'char s[], int lim' line is redefining what 's' and
'lim' were in the first place.

	First question:  How does one pass variables to a function in
Laser C, and how do you declare that the function will return a
certain type?

	Second question: Is there a Laser C upgrade which meets the
ANSI standards?  I have heard a lot of good stuff about this language
and find it hard to believe that it does not meet a standard that was
set over two years ago.

	My apologies if I've been verbose with my problem;  I wanted
to insure that I was specific in defining it.

		Thanx in advance to any Laser C 'experts' out there!

			Perry Pederson    n8742883@unicorn.WWU.EDU

gordon@osiris.cso.uiuc.edu (John Gordon) (07/13/90)

	Do you have a function prototype?  If so, do not name the parameters
in it, just list the types:

void foo(int, int, char [])  rather than
void foo(int x, int y, char cow[])

	Hope this helps.


---
John Gordon
Internet: gordon@osiris.cso.uiuc.edu        #include <disclaimer.h>
          gordon@cerl.cecer.army.mil       #include <clever_saying.h>
GEnie:    j.gordon14                  

wallace@ynotme.enet.dec.com (Ray Wallace) (07/13/90)

In article <1213@unicorn.WWU.EDU>, n8742883@unicorn.WWU.EDU (Perry Pederson) writes...
> 
>	It appears to me that Laser C is _not_ ANSI compatible, which
>really burns me.  I got massive syntax errors when I tried to compile

>/* getline; read a line into s, return length */
> 
>getline(s, lim)
>char s[], int lim;
>{
>  etc. etc. etc. 
>}
You can fix this one by changing the comma in the second line, to a semicolon.
More commonly (I think) is to put the different types on seperate lines. Ie:
char	*s;
int	lim;

>	First question:  How does one pass variables to a function in
>Laser C, and how do you declare that the function will return a
>certain type?
Ahh, thats two questions? When you define a function, place only the variable
names (not the types) inside the paranthesis (after the function name). To
specify the data type returned by the function, just place the type in front
of the function name. Ie:
FILE fopen( name, mode )
char	*name, *mode;		/* The comma is correct here since both
				 * variables are of the same type.
				 */
{
  static FILE file1;
    etc. etc. etc.
  return &file1;
}

> 
>	Second question: Is there a Laser C upgrade which meets the
>ANSI standards?  I have heard a lot of good stuff about this language
I don't know the answer to that.

>		Thanx in advance to any Laser C 'experts' out there!
I have never used Laser C, but you are welcom anyway :-)

---
Ray Wallace		
		(INTERNET,UUCP) wallace@oldtmr.enet.dec.com
		(UUCP)		...!decwrl!oldtmr.enet!wallace
		(INTERNET)	wallace%oldtmr.enet@decwrl.dec.com
---

alex@athertn.Atherton.COM (Alex Leavens) (07/14/90)

Perry Pederson asks: 


>        First question:  How does one pass variables to a function in
>Laser C, and how do you declare that the function will return a
>certain type?

>  I have tried to 'mimic' their
>example of 'older' versions of C programming by declaring the
>variables under the function declaration and removed the beginning
>'int', thus reading:

>/* getline; read a line into s, return length */

>getline(s, lim)
>char s[], int lim;
>{
>  etc. etc. etc.
>}

Your example should read as follows:

getline(s, lim)
char s;
int lim;
{

stuff here

}

The problem with your example is that you have a comma following
the definition of s[], not a semi-colon.  That ain't Laser C's
fault, that's the C language--you canna do that, lad.  <grin>

I don't know if Laser C has an ANSI upgrade--Mark Williams has
been promising one for a while, but so far has pooped out on
actually producing it....



-- 
|-------------------------------------------------------------------------|
|--alex | alex@Atherton.COM |  Caution!  Falling Opinions, next 6 miles   |
|     New Net Address!!: UUCP: {uunet,ucbvax}!unisoft!bdt!dsdeng!alex     |
|                   "AAAAAGH!  It's Friday the 13th!!!                    |

ALBERT_DAYES@bdt.UUCP (07/16/90)

  Megamax has a phone (214) 699-7400, and a BBS (214) 699-0972 (1200 baud) ...
  hopefully we will have an ANSI compatible LASER C compiler by Christmas.

john@its.bt.co.uk (John Trickey) (07/19/90)

In article <1213@unicorn.WWU.EDU> n8742883@unicorn.WWU.EDU (Perry Pederson) writes:
>
>int getline(char s[], int lim)
>{

Your array of char will have been pre-defined in some other part of the
code by:-  char string[strint_length]; so at this point you will be
responding to a fn call of getline(string, string_limit);
This means you need a "pointer to a char" and not an array in the fn
definition which should be:-

	int getline(s,lim)
	char *s;
	int lim
	{

These are interchangeable at reference time but not at definition time
so the remainder of your code can remain the same ... except

>getline(s, lim)
>char s[], int lim;
>{

This would not work as described before but in terms of C programming
is a simple error.  char a, b, c; is a single statement.  If you
tried char a, int, b; it would try to create a char variable called int
if it did not get confused.  To achieve your end you need two statements
as in char s[]; int lim;

I hope this has helped you to understand why your compiler complained.

John

-- 

John Trickey                      British Telecom Applied Systems.
jvt@its.bt.co.uk                  ..!mcsun!ukc!axion!its
G4REV @ GB7SUT                    Voice: +44 21 333 3369