Thread: Couldn't stop!

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    6

    Question Couldn't stop!

    Problems:
    Let's say that there are 3 arrays sized 20bytes, 13 bytes and 40 bytes respectively, I need to truncate/read these arrays, 5 bytes one by one and continue to truncate/read another 5 bytes once the loop repeats. Below is the layout:

    array no first loop second loop third loop.......
    1 5 bytes 5 bytes 5 bytes.....
    2 5 bytes 5 bytes 5 bytes.....
    3 5 bytes 5 bytes 5 bytes.......

    I had written a source code to do this task but the problem is that it still continues to read 5 bytes eventhough it is the end of the array.
    In this case, the second array of course will reach the end of array first.
    This will cause some funny result. How do we stop reading the second array but in other hand, continues to read until the end of other arrays.
    I had tried to stop the array that will ends first but it repeats reading when the loop, loops again.
    Please help!

    code

    #include <stdio.h>
    #include <stdlib.h>

    void main()
    {
    int a, b, c;

    //Declare and initialised array of pointers.
    char *array[3]={"My name is David",
    "Hello world",
    "cockroach is smelly"};

    //Truncate the array
    .
    for(a=0; a<5; a+=5)
    /*A loop that steps through the array of arrays 5 times until it reaches 20.*/

    {
    /*A loop that steps through the arrays.*/
    for(b=0; b<3; b++)
    {

    /*A loop that steps through the array of arrays 5 times starting at the point 'a' i.e. the variable of the first loop.*/
    for(c=a; c<(a+5); c++)
    {
    printf("%c\n", array[b][c]);
    }
    }
    }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Like so?
    Code:
    #include <stdio.h>
    #include <string.h>
    int main() {  // yes, REALLY!!!
        int a, b, c;
        //Declare and initialised array of pointers.
        char*array[3]={"My name is David",
            "Hello world",
            "cockroach is smelly"};
    
        /*A loop that steps through the array of arrays 5 times until it reaches 20.*/
        for(a=0; a<20; a+=5) {
            /*A loop that steps through  the arrays.*/
            for(b=0; b<3; b++) {
                /*A loop that steps through the array of arrays 5 times starting at the point 'a' i.e. the variable of the first loop.*/
                for(c=a; c<(a+5); c++) {
                    if ( c < strlen(array[b]) ) printf( "%c", array[b][c] );
                }
                printf( "\n" );
            }
            printf( "-----\n" );
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fscanf %s or %d integer input space char stop question...
    By transgalactic2 in forum C Programming
    Replies: 5
    Last Post: 04-14-2009, 10:44 AM
  2. Error stop Http Listener
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 06-04-2008, 02:14 AM
  3. error stop a Windows service
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-30-2008, 03:26 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. Telling other applications to stop
    By nickname_changed in forum Windows Programming
    Replies: 11
    Last Post: 09-25-2003, 12:47 AM