home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!mcsun!sun4nl!and!jos
- From: jos@and.nl (Jos Horsmeier)
- Newsgroups: comp.lang.c
- Subject: Re: Help: problem codes
- Message-ID: <3362@dozo.and.nl>
- Date: 31 Aug 92 08:38:47 GMT
- References: <1992Aug31.003108.9782@uoft02.utoledo.edu>
- Organization: AND Software BV Rotterdam
- Lines: 61
-
- In article <1992Aug31.003108.9782@uoft02.utoledo.edu> qding@uoft02.utoledo.edu writes:
- |
- |Hi, there.
- |I have a C program (shown below) which shows error
- |of >> Illegal structure operation in fuction sum. But I just
- |do not understand what's wrong with it. Can some one help out of
- |this please ?
- |
- |C codes ----
- |
- |typedef double tuple[4];
- |
- |typedef struct vector {
- | integer dim; /* the dimension of the vector */
- | tuple component; /* the array of components */
- |} vector; /* the definition of type vector */
- |
- |/* function sum */
- |vector sum(vector a, vector b)
- |{
- | integer i;
- | sum.dim = a.dim;
- | for (i = 1; i <= a.dim; i++)
- | sum.component[i] = a.component[i] + b.component[i];
- |} /* function sum */
-
- Welcome Pascalien! Welcome to the wonderful world of C! ;-)
- No, seriously, I strongly suggest reading K&R2 before you start
- coding in C. You've just done a basic translation from Pascal to C,
- and things don't work that way. A few hints:
-
- - non void functions (Pascal: non procedures) use the `return' statement
- to return a value to the caller.
-
- - the name of a function is _not_ a left hand value.
-
- - array offsets start at 0 in C, not at 1 as in Pascal.
-
- - an integral number type is called `int', not `integer' as in Pascal.
-
- Here's your function again, a bit rewritten:
-
- vector sum(vector a, vector b)
- {
- int i;
- vector sum_val;
-
- sum_val.dim = a.dim;
- for (i = 0; i < a.dim; i++)
- sum_val.component[i] = a.component[i] + b.component[i];
-
- return sum_val;
-
- } /* function sum */
-
- Please do read a good text book (take K&R2), it'll save you a lot
- of trouble.
-
- kind regards,
-
- Jos aka jos@and.nl
-