I'm a bit puzzled. I'm new to C and am looking at some C code but am
stuck. I can't figure it out, specifically the call [to] the "Weight"
array without [an index?]
register float Sum=0.0, *A, *B;
whoever wrote this is using some odd coding conventions.
I'd not trust this source too highly.
register is almost useless with modern compilers. It is a
hint to the compiler that Sum is "important". Many compilers
ignore the hint. It could even make your code *slower*.
It is usually better to use double rather than float.
They give nearly the same performance and double has
more guranteed precison. float may be smaller but
this rarely matters. It is unusual to use anything other
than all lower case for identifiers. All upper case
is usually reserved for macros. Some people use initial
upper case for types. It's a bad idea to put multiple
definitions on one line. He also seems to not like whitespace.
So his code is all squished up and hard to read.
so I'd code the above as
double sum = 0.0;
double *a;
double *b;
wow. a single line of code can generate a paragraph of comment!
for ( A = Weight + Fp, B = Weight + Lp ; A <= B; )
{
Sum += *A++;
}
to the best of my knowledge "Weight" is an array of floats.
why "to the best of your knowledge"? Can't you find the definition
of Weight?
But I do
not understand how it can be accessed without
an index like that above?
it's a trick of C.
a
is actually defined to be equivalent to
*(&a[0] + i). That is indexing is the equivalent of dereferencing
(finding the value stored in the location) the sum of the base of
the array and the index. When adding an index to a pointer
the index is multiplied by the size of the array elements.
hence
char ca[5];
char *cp;
double da[5];
double *dp;
cp = ca + 3;
dp = da + 3;
"works", both pointers end up pointing to the fourth (arrays
are indexed from 0 in C) entry of the respective array.
In the char case that just means add 3 (a char is always of
size 1 in C) and in the double case add 3 * sizeof(double).
This could be regarded as a useful technique or an over clever trick.
He could have written
A = &Weight[Fp]; instead of A = Weight + Fp;
They mean exactly the same thing (and almost certainly generate the
same code). Use whichever you find plainer. I'd probably follow the
code you posted. But I don't like this
current_wt = *(Weight + Cw);
that's just being obscure. Do this
current_wt = Weight [Cw];
Is that how you get the length of an array?
nope.
Or does it somehow cycle through the elements of the array somehow?
yes, it's indexing in disguise
Also the line: Sum += *A++; is a bit tight for me. Any ideas?
X++ yields the value of X, then it increments X. *X dereferences
(gets the value from) a pointer. X += Y adds Y to X. So picking the
above apart:
value_a = *A; /* get the value from the pointer */
Sum = Sum + value_a;
A = A + 1; /* advance A to next element in the array */
Would greatly appreciate any comments/suggestions/code-samples as my
main language is C#
1. don't use the reference you found the posted code in.
2. find a good book eg. K&R "The C Programming Language"
- they explain things much better than I could
3. do the exercises in K&R
4. write programs
5. write programs
6. post here with any questions
7. read the comp.lang.c FAQ
8. write programs
(...)
aleph-0. write programs
aleph-1. observe heat death of universe
--
Nick Keighley
That's the thing about people who think they hate computers.
What they really hate is lousy programmers.
- Larry Niven and Jerry Pournelle in "Oath of Fealty"