Thread: Array output not as expected

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    Tater,

    Code:
    *t[i] = 0;
    The above code is not proper pointer syntax. Just to verify this, I made changes to my source, and it errors out.

    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:
    Code:
    double total[6];
    to this:

    Code:
    double *total;
    in order to comply with pointer syntax.

    When changed to the latter, it causes a failure in the initialization:

    Code:
    init_total(total);

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Futomara
    The question is about whether the total array must be changed from: (...) in order to comply with pointer syntax.
    That question should be posed to your instructor.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    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.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Futomara
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Take 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...
    Since t is a pointer. The following code does properly initialize the double using pointer addition

    However to make the program more robust the size of the array should be passed into this function.

    Code:
    void init_total(double *t, int array_size)
    {
    	int i;
    	for (i=0; i < array_size; i++)
    		*(t+i) = 0;
    }
    Jim
    Last edited by jimblumberg; 11-03-2010 at 11:30 AM.

  7. #22
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    @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.

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Futomara
    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.
    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:
    Code:
    void init_total(double *t, size_t array_size)
    {
        size_t i;
        for (i = 0; i < array_size; i++)
            t[i] = 0;
    }
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    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?

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Futomara
    One thing that is unclear is why pointers shouldn't be used.
    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.

    Quote Originally Posted by Futomara
    I understand they are better for memory management. So, why not always use them?
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Lots of other ways to [ab]use pointers than the (ugly, no argument there) *(t+i) syntax :

    Code:
    void init_total(double *t, size_t array_size)
    {
        while(array_size--)
            *t++ = 0.0;
    }
    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.

  12. #27
    Registered User
    Join Date
    Oct 2010
    Posts
    29
    @KCfromNC - Now that's pretty darned elegant. I haven't seen that way before.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Modify an single passed array element
    By swgh in forum C Programming
    Replies: 3
    Last Post: 08-04-2007, 08:58 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM