![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
Value of index incorrect after loop
I'm playing around with some of the string functions to see how they work and I came across this odd problem in my code. The value of i after my while loop exits is reset to 0 even though the last number in the loop is 3. I'm pretty sure that the value should not be 0 but after studying for the last 2 hours I'm not sure if my brain can see the problem. Please help me out.
#include <stdio.h>
#include <string.h>
int main(void) {
char input[21];
char *name[3];
int i = 1;
printf("Please enter you name: ");
gets(input);
if ((name[0] = strtok(input, " ")) != NULL) {
while ((name[i] = strtok(NULL, " ")) != NULL && i < 3) {
printf("%d\n", i); /* i is 3 at end of loop when */
/* john jacob smith is entered */
i++;
}
}
printf("%d\n", i); /* i is 0 after loop, why? */
while (--i >= 0) {
switch (i) {
case 0: printf("First Name: %s\n", name[i]);
printf("Length: %d\n", strlen(name[i]));
break;
case 1: printf("Middle Name: %s\n", name[i]);
printf("Length: %d\n", strlen(name[i]));
break;
case 2: printf("Last Name: %s\n", name[i]);
printf("Length: %d\n", strlen(name[i]));
break;
}
}
return 0;
} |
|
|
|
|
|
#2 |
|
Programmer
|
what's the function 'strtok()'?I can'tfind it in my man file?
|
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5
![]() |
i couldn't figure it out either...so i ran it. you can stop scratching your head. it is NOT reset to zero. it prints "1 2 3" where the "3" is the last printf outside of the loop. i modified NOTHING. now you can sleep. :p
__________________
i put on my robe and wizard hat... Have you ever heard of Plato, Aristotle, Socrates?...Morons. |
|
|
|
|
|
#4 | ||
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
Quote:
Quote:
|
||
|
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
So I ran the code on my college's Linux server and it works properly. I guess there's some funky stuff between the gcc compiler and the AIX compiler.
|
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
For those who haven't used "strtok", be aware that it works with a static pointer (to keep track between calls) and modifies the original string (dumps terminators into it). Be sure and copy the original string if you want to preserve it.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 | |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
Quote:
![]() |
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Actually, you could have just read the docs, but I figured (how did I guess?) a lot of people don't.
![]()
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#9 |
|
Expert Programmer
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4
![]() |
I've read the docs
*proud* |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
Actually, I never heard of strtok() until Friday when one of my professors mentioned it. I checked it in my book The C Programming Language and followed that documentation.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|