home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / c / 13009 < prev    next >
Encoding:
Internet Message Format  |  1992-08-30  |  1.9 KB

  1. Path: sparky!uunet!mcsun!sun4nl!and!jos
  2. From: jos@and.nl (Jos Horsmeier)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help: problem codes
  5. Message-ID: <3362@dozo.and.nl>
  6. Date: 31 Aug 92 08:38:47 GMT
  7. References: <1992Aug31.003108.9782@uoft02.utoledo.edu>
  8. Organization: AND Software BV Rotterdam
  9. Lines: 61
  10.  
  11. In article <1992Aug31.003108.9782@uoft02.utoledo.edu> qding@uoft02.utoledo.edu writes:
  12. |
  13. |Hi, there.
  14. |I have a C program (shown below) which shows error
  15. |of >> Illegal structure operation in fuction sum. But I just
  16. |do not understand what's wrong with it. Can some one help out of
  17. |this please ?
  18. |
  19. |C codes ----
  20. |
  21. |typedef double tuple[4];
  22. |
  23. |typedef struct vector {
  24. |    integer      dim; /* the dimension of the vector */
  25. |    tuple        component; /* the array of components */
  26. |} vector; /* the definition of type vector */
  27. |
  28. |/* function sum */
  29. |vector sum(vector a, vector b)
  30. |{
  31. |   integer      i;
  32. |   sum.dim = a.dim;
  33. |   for (i = 1; i <= a.dim; i++)
  34. |   sum.component[i] = a.component[i] + b.component[i];
  35. |} /* function sum */
  36.  
  37. Welcome Pascalien! Welcome to the wonderful world of C! ;-)
  38. No, seriously, I strongly suggest reading K&R2 before you start
  39. coding in C. You've just done a basic translation from Pascal to C,
  40. and things don't work that way. A few hints:
  41.  
  42. - non void functions (Pascal: non procedures) use the `return' statement
  43.   to return a value to the caller.
  44.  
  45. - the name of a function is _not_ a left hand value.
  46.  
  47. - array offsets start at 0 in C, not at 1 as in Pascal.
  48.  
  49. - an integral number type is called `int', not `integer' as in Pascal.
  50.  
  51. Here's your function again, a bit rewritten:
  52.  
  53. vector sum(vector a, vector b)
  54. {
  55.    int    i;
  56.    vector sum_val;
  57.  
  58.    sum_val.dim = a.dim;
  59.    for (i = 0; i < a.dim; i++)
  60.    sum_val.component[i] = a.component[i] + b.component[i];
  61.  
  62.    return sum_val;
  63.  
  64. } /* function sum */
  65.  
  66. Please do read a good text book (take K&R2), it'll save you a lot
  67. of trouble. 
  68.  
  69. kind regards,
  70.  
  71. Jos aka jos@and.nl
  72.