954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problems with "strtok" function

Hi

I am trying to make my homework about a command line calculator, and i am trying to divide the entered string into small parts and i am trying to use strtok for this.

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct var
{
char name[10];
double value;
};



void main()
{
char in[100], *temp[10],type;
int i,j,k,temp1,temp2,temp3;
k=0;

printf("Enter the expression >>");
gets(in);

temp[0]=strtok(in," ");
for (i=1;temp[i]!=NULL;i++)
{
temp[i]=strtok(NULL," ");
}

for(k=0;k<10;k++)
{
printf("%s",temp[k]);
}

}


But when i try to execute, it gives me the error "Run-Time Check Failure #2 - Stack around the variable 'temp' was corrupted." and writes 7 times instead of divided string parts. How can i fix it? Or is there any other way to do the same thing.
By the way in my homework, user gives expressions with spaces in between, for example NOT "3+5" but "3 + 5".

Thanks for any help

gkaykck
Light Poster
40 posts since Aug 2009
Reputation Points: 17
Solved Threads: 2
 

It's an order of operations problem. Inside the loop, you're always testing the next pointer in temp, which is always uninitialized. Take some time to test out how for loops manage the counter variable, because a lot of code relies on that behavior and it's easy to write code that makes false assumptions like this one.

Here's an improved version:

int main(void)
{
    char in[100], *temp[10];
    int i, n = 0;

    printf("Enter the expression: ");
    fflush(stdout);
    
    if (fgets(in, sizeof in, stdin) != NULL)
    {
        temp[0] = strtok(in, " ");

        if (temp[0] != NULL)
        {
            for (n = 1; n < 10 && (temp[n] = strtok(NULL," ")) != NULL; n++)
                ;
        }

        for (i = 0; i < n; i++)
        {
            printf("%s\n", temp[i]);
        }
    }

    return 0;
}

Note the call to fgets (a vast improvement over the unsafe gets) and the fact that input is being tested for success or failure. Next is the loop. Instead of calling subsequent strtoks in the body, it's done in the condition where the condition can be checked before n is updated.

I used n in the first loop to indicate how many strings there are. You could also use NULL as long as temp is initialized to all NULLs first. The second loop uses n as the end case.

Another very important thing to notice is how the first loop also takes care not to exceed the size of temp if there are more than ten tokens. Noticing and covering for errors like this comes with experience, but you should always have an eye for failure points.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

thank you, i'll be more careful next time :)

gkaykck
Light Poster
40 posts since Aug 2009
Reputation Points: 17
Solved Threads: 2
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: