iteration revisited

This is a discussion on iteration revisited within the C Programming forums, part of the General Programming Boards category; Code: /*writes a (non-NULL terminated) char array to the file f*/ void write_ch_arr(char *c, int i, FILE *f) { while ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129

    iteration revisited

    Code:
    /*writes a (non-NULL terminated) char array to the file f*/
    void write_ch_arr(char *c, int i, FILE *f)
    {
        while (i)
        {
            putc(c[i], f);
            --i;
        }
        return;
    }
    now the problem with this is that theoretically, since c can span the entire address range, int might not hold all possible addresses of c.

    so what type should I make i?
    is the only way to do this with pointer arithmetic? Can I do it with an index and iteration?

    of course, this ignores the inefficiency of byte vs. block based transfers.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If you want to use an index, I would recommend size_t as your data type.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, size_t is guaranteed to cover the same range as a pointer can do in that architecture, so indexing using a size_t type should be fine. size_t is unsigned, which can be useful sometimes and annoying at other times.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Iteration?
    By Aliaks in forum C++ Programming
    Replies: 2
    Last Post: 06-06-2009, 11:17 PM
  2. What is the difference between Recursion and Iteration?
    By neo_phyte in forum C Programming
    Replies: 12
    Last Post: 09-13-2006, 02:13 AM
  3. MPI - linear pipeline solution for jacobi iteration
    By eclipt in forum C++ Programming
    Replies: 1
    Last Post: 05-03-2006, 05:25 AM
  4. recursivity & iteration
    By Marlon in forum C++ Programming
    Replies: 2
    Last Post: 06-14-2005, 01:58 PM
  5. The "continue" statement in JAVA
    By Hexxx in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 02-26-2004, 05:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21