#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    9
    Rep Power
    0

    How do make a for loop loop till null character of array


    how do i make a for loop loop all the way through until it gets before the null character of an array

    i want to print something using a for loop and array

    so i got an array of a word from a file
    for example the word year
    but there are other words in the file with different lengths

    now I know
    that the word year gets stored into my word[] array and i think word[4] is where the null character \0 is.


    now i want to print an x for the length of the word but i want it to go up to until before the null character


    for(i=0;i<word[NULL];i++){

    printf("x");

    }

    so i want i to print all the way up to before the null character of the word that gets stord into my word[]array
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,163
    Rep Power
    2222
    Remember that zero is false and non-zero is true. And that you remain in the for loop as long as the test expression is true (ie, non-zero). And that the null character has an ASCII code of zero and so would be considered false, while all other characters are non-zero and would be considered true.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by program57
    how do i make a for loop loop all the way through until it gets before the null character of an array

    i want to print something using a for loop and array

    so i got an array of a word from a file
    for example the word year
    but there are other words in the file with different lengths

    now I know
    that the word year gets stored into my word[] array and i think word[4] is where the null character \0 is.


    now i want to print an x for the length of the word but i want it to go up to until before the null character


    for(i=0;i<word[NULL];i++){

    printf("x");

    }

    so i want i to print all the way up to before the null character of the word that gets stord into my word[]array
    while(*word++)
    {
    printf("x");
    }

    word is a pointer and null is zero so you can increment address one by one and you dereference the pointer until you reach zero.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,163
    Rep Power
    2222
    Originally Posted by serafon
    while(*word++)
    {
    printf("x");
    }

    word is a pointer and null is zero so you can increment address one by one and you dereference the pointer until you reach zero.
    No, word is an array, not a pointer. You cannot increment or decrement an array name. As we are taught: array names are for the most part equivalent to pointers. This is one of the cases where that general rule does not apply.

    Rather, declare a char pointer and initialize it to word, then increment the pointer.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    13
    Rep Power
    0
    Originally Posted by dwise1_aol
    No, word is an array, not a pointer. You cannot increment or decrement an array name. As we are taught: array names are for the most part equivalent to pointers. This is one of the cases where that general rule does not apply.

    Rather, declare a char pointer and initialize it to word, then increment the pointer.
    Ah you are right, sorry!

    he should do

    Code:
    char *ptr = word;
    while(*ptr++)
      printf("x");
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,388
    Rep Power
    1871
    This also works
    for(i=0;word[i] != '\0';i++)
    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

IMN logo majestic logo threadwatch logo seochat tools logo