The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
How make a for loop
Discuss How make a for loop in the C Programming forum on Dev Shed. How make a for loop C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 6th, 2012, 07:13 PM
|
|
Registered User
|
|
Join Date: Sep 2012
Posts: 9
Time spent in forums: 3 h 42 m 16 sec
Reputation 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
|

November 6th, 2012, 07:36 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 2 h 1 m 44 sec
Reputation Power: 0
|
|
Quote: | 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.
|

November 7th, 2012, 12:17 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
Quote: | 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.
|

November 7th, 2012, 04:27 AM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 13
Time spent in forums: 2 h 1 m 44 sec
Reputation Power: 0
|
|
Quote: | 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");
|

November 7th, 2012, 04:59 AM
|
 |
Contributed User
|
|
|
|
|
This also works
for(i=0;word[i] != '\0';i++)
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|