Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 4th, 2005, 11:21 PM   #1
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
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;
}
aznluvsmc is offline   Reply With Quote
Old Nov 5th, 2005, 12:13 AM   #2
sinix
Programmer
 
sinix's Avatar
 
Join Date: Nov 2005
Location: China
Posts: 47
Rep Power: 0 sinix is on a distinguished road
Send a message via MSN to sinix
what's the function 'strtok()'?I can'tfind it in my man file?
sinix is offline   Reply With Quote
Old Nov 5th, 2005, 1:32 AM   #3
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
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.
bl00dninja is offline   Reply With Quote
Old Nov 5th, 2005, 9:24 AM   #4
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
Quote:
Originally Posted by sinix
what's the function 'strtok()'?I can'tfind it in my man file?
strtok() is part of <string.h>. It takes a string and splits it into tokens on any character speficied in string2.


Quote:
Originally Posted by bl00dninja
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
That's odd. I decided to delete my old file and just copy what I posted into a new one a run it. It still prints "1 2 0". Maybe it's because I'm running the code on AIX? I'll test it on Linux to see if it makes a difference.
aznluvsmc is offline   Reply With Quote
Old Nov 5th, 2005, 5:29 PM   #5
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
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.
aznluvsmc is offline   Reply With Quote
Old Nov 5th, 2005, 6:45 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 5th, 2005, 9:58 PM   #7
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
Quote:
Originally Posted by DaWei
Be sure and copy the original string if you want to preserve it.
Yes, I had to learn this the hard way.
aznluvsmc is offline   Reply With Quote
Old Nov 6th, 2005, 7:03 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Nov 6th, 2005, 7:05 AM   #9
Polyphemus_
Expert Programmer
 
Polyphemus_'s Avatar
 
Join Date: Aug 2005
Location: Rotterdam, the Netherlands
Posts: 942
Rep Power: 4 Polyphemus_ is on a distinguished road
I've read the docs *proud*
Polyphemus_ is offline   Reply With Quote
Old Nov 6th, 2005, 11:29 AM   #10
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
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.
aznluvsmc is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:13 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC