strtok problem

J

jorntk

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

void strlines(char line[], char tokens[])
{
char * token_ptr, token[81];
token_ptr = strtok(line, " ");
while(token_ptr)
{
strcpy(token,token_ptr);
strcat(tokens, strcat(token, "\n"));
token_ptr=strtok(NULL, " ");
}
}
int main()
{
char sentence[81], words[81];
printf("enter a sentence: ");
fgets(sentence, sizeof sentence, stdin);
strlines(sentence,words);
puts(words);
}

why the above code display ,X @ at the beginning of the output?
 
B

Ben Pfaff

void strlines(char line[], char tokens[])

This function appends to tokens[], but doesn't initialize it.
int main()
{
char sentence[81], words[81];
printf("enter a sentence: ");
fgets(sentence, sizeof sentence, stdin);
strlines(sentence,words);

This functions passes words[] to strlines() without initializing
it.
puts(words);

You should return a value from main().
}

why the above code display ,X @ at the beginning of the output?

Is it really surprising given the failure to initialize?
 
B

Barry Schwarz

void strlines(char line[], char tokens[])

This function appends to tokens[], but doesn't initialize it.
int main()
{
char sentence[81], words[81];
printf("enter a sentence: ");
fgets(sentence, sizeof sentence, stdin);
strlines(sentence,words);

This functions passes words[] to strlines() without initializing
it.

But words is converted to a pointer to the first element of the array
so there is no reference at this point to an uninitialized variable.
You should return a value from main().


Is it really surprising given the failure to initialize?



<<Remove the del for email>>
 
B

Barry Schwarz

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

You shouldn't use str as the prefix for a function name you are
defining. It is reserved.
void strlines(char line[], char tokens[])
{

At this point tokens, which is treated as a pointer in this context,
points to the first element of words in main. Unfortunately, that
element has not yet been initialized.
char * token_ptr, token[81];
token_ptr = strtok(line, " ");
while(token_ptr)
{
strcpy(token,token_ptr);
strcat(tokens, strcat(token, "\n"));

strcat requires that the first argument (the destination) point to a
well formed string. tokens does not. I points to the uninitialized
array words in main.

You can initialize words where it is defined in main, by code in main,
or by initializing tokens[0] to '\0' in strlines.
token_ptr=strtok(NULL, " ");
}
}
int main()
{
char sentence[81], words[81];
printf("enter a sentence: ");
fgets(sentence, sizeof sentence, stdin);
strlines(sentence,words);
puts(words);
}

why the above code display ,X @ at the beginning of the output?

You were unlucky. On a good day, the first call to strcat would have
caused your program to abort. Basically, strcat started looking for a
'\0' and eventually found one just a couple of bytes from the tokens
was pointing. The very nature of strcat caused these uninitialized
bytes to remain unchanged during the life of your program.



<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top