I suggest that you post the program again, but without using pointer syntax unnecessarily. Once your program is working completely, then you can convert it to use pointer syntax for fun if that is the silly requirement.
I suggest that you post the program again, but without using pointer syntax unnecessarily. Once your program is working completely, then you can convert it to use pointer syntax for fun if that is the silly requirement.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Tater,
The above code is not proper pointer syntax. Just to verify this, I made changes to my source, and it errors out.Code:*t[i] = 0;
I only need to initialize each element and then I am accumulating the contents of each position as it is used.
The question is about whether the total array must be changed from:
to this:Code:double total[6];
in order to comply with pointer syntax.Code:double *total;
When changed to the latter, it causes a failure in the initialization:
Code:init_total(total);
That question should be posed to your instructor.Originally Posted by Futomara
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I prefer to ask it here where I can get an answer I can understand. Plus, I won't always have an instructor. What then? Exactly, I'll be back here asking for help.
Ah, but when you don't have an instructor, you don't need to care about silly requirements like this. Basically, creating an array is not pointer syntax. But an if statement is not pointer syntax too. So, what exactly is the requirement? I interpret it as "avoid the use of array index syntax", but maybe your instructor thinks otherwise. Hence, ask your instructor. This is one of those questions that is purely for a school assignment; in the real world you would not need to worry about it.Originally Posted by Futomara
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Since t is a pointer. The following code does properly initialize the double using pointer additionTake note that your loop is only initializing the first 6 bytes of the array if doubles. If total is an array, you need to treat it like one. This should work...
However to make the program more robust the size of the array should be passed into this function.
JimCode:void init_total(double *t, int array_size) { int i; for (i=0; i < array_size; i++) *(t+i) = 0; }
Last edited by jimblumberg; 11-03-2010 at 11:30 AM.
@laserlight - My expectation is to learn the best coding practices, whether it be instructor 'taught' or through self-teaching. I generally learn better with the latter. However, it is always nice to have a place to get answers. I just want to learn the proper way to code.
@Jim - I understand your point and did make the changes. Thanks for that.
That's good. The proper way to code in this case is to use array index syntax, i.e., jimblumberg's example in post #21 should be:Originally Posted by Futomara
Notice that I left t as a pointer to double, because that is what it is. If you prefer, you could have declared it as double t[], but it would still be a pointer, not an array. Actually, in this case you could use memset instead of manually writing a loop.Code:void init_total(double *t, size_t array_size) { size_t i; for (i = 0; i < array_size; i++) t[i] = 0; }
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
One thing that is unclear is why pointers shouldn't be used. I understand they are better for memory management. So, why not always use them?
I did not say that pointers should not be used. I said that you should use array index syntax. t[i] is equivalent to *(t+i), but it is easier to read.Originally Posted by Futomara
It is not true that they are "better for memory management". They are necessary for the use of malloc and related functions in order to say, implement a dynamic array, but it is not always necessary to use them.Originally Posted by Futomara
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Lots of other ways to [ab]use pointers than the (ugly, no argument there) *(t+i) syntax :
It's a toss up which is better. For something this simple it's a pretty common idiom to see this sort of code. I agree be less likely to see *(t+i) = 0, but for quick trips through an array like this, I think this syntax is just as common as any other reasonable version.Code:void init_total(double *t, size_t array_size) { while(array_size--) *t++ = 0.0; }
@KCfromNC - Now that's pretty darned elegant. I haven't seen that way before.