November 6th, 2012, 07:13 PM
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 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
so i want i to print all the way up to before the null character of the word that gets stord into my wordarray
November 6th, 2012, 07:36 PM
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.
November 6th, 2012, 11:01 PM
Originally Posted by program57
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.
November 7th, 2012, 12:17 AM
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.
Originally Posted by serafon
Rather, declare a char pointer and initialize it to word, then increment the pointer.
November 7th, 2012, 04:27 AM
Ah you are right, sorry!
Originally Posted by dwise1_aol
he should do
char *ptr = word;
November 7th, 2012, 04:59 AM
This also works
for(i=0;word[i] != '\0';i++)