Here is my code, just to test the strtok functionality: - #include <stdio.h>
-
#include <stdlib.h>
-
#include <string.h>
-
-
struct sampleClass
-
{
-
int num;
-
char *text;
-
struct sampleClass *next;
-
};
-
-
typedef struct sampleClass node;
-
-
void fillinnode(node *target, int num, char **text);
-
void printoutnodes(node *startnode);
-
-
-
int main()
-
{
-
char text[30];
-
char *token1, *token2, *token3;
-
int i;
-
-
node *a=(node *) malloc(sizeof(node));
-
node *b=(node *) malloc(sizeof(node));
-
node *c=(node *) malloc(sizeof(node));
-
-
a->next = b;
-
b->next = c;
-
c->next = NULL;
-
-
for(i=0; i<30; i++)
-
{
-
if (i%10 == 0&&i!=0)
-
text[i]=':';
-
else text[i]='*';
-
}
-
-
printf("original string is");
-
for(i=0; i<30; i++)
-
printf("%c", text[i]);
-
printf("\n");
-
-
token1 = strtok(text, ":");
-
printf("token1=%s\n", token1);
-
-
token2 = strtok(NULL, ":");
-
printf("token2=%s\n", token2);
-
-
token3 = strtok(NULL, "");
-
printf("token3=%s\n", token3);
-
-
fillinnode(a, 1, &token1);
-
fillinnode(b, 2, &token2);
-
fillinnode(c, 3, &token3);
-
-
printoutnodes(a);
-
}
-
-
void fillinnode(node *target, int num, char **text)
-
{
-
target->num = num;
-
target->text = *text;
-
}
-
-
void printoutnodes(node *startnode)
-
{
-
node *temp = startnode;
-
while(temp!= NULL)
-
{
-
printf("%d", temp->num);
-
printf("%s", temp->text);
-
temp = temp->next;
-
}
-
}
It compiled well with gcc and runs. However, the last string token is not closed well, see the output: - charlie$ gcc why.c
-
charlie$ ./a.out
-
original string is**********:*********:*********
-
token1=**********
-
token2=*********
-
token3=*********
-
1**********2*********3*********��charlie$
-
In my linux terminal, the end of token is displayed with a '(?)' symbol, which probably means this token is not closed well. How to fix this problem?
2 3372
Problem 1: Your text string has no null terminator. strtok uses that to tell where your string ends:
for(i=0; i<30; i++)
{
if (i%10 == 0&&i!=0)
text[i]=':';
else text[i]='*';
}
text[i] = '\0'; <<<< I added this.
Problem 2: your text array ends up with 33 characters in it and you allocated only 30. This causes a crash when main() returns. I changed text to an array of 40 and the crash went away.
Clearly, it's time for you to start learning how to use your debugger.
Thanks a lot. I've decided rewrite my code to fix this problem. The null terminator is really a big issue to think about...
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Lans |
last post by:
I have a string that I need to tokenize but I need to use a string
token
see example i am trying the following but strtok only uses characters
as delimiters and I need to seperate bu a certain...
|
by: daniel |
last post by:
Hi everyone,
I'm trying to get this program compiled under Solaris. I have actually
no idea about programming, sorry to bother you.
Unfortunately Solaris don't use the function strsep()...
|
by: collinm |
last post by:
hi
this is my code to analyse a file
void analyzeFilename()
{
char string="B_L2_HLD_GRN_NOR_Run_Counter.txt";
char *tokenptr;
char *seperators="_";
|
by: daniel |
last post by:
Hi everyone,
I'm trying to get this program compiled under Solaris. Unfortunately I
have little experience with C.
Solaris doesn't use the function strsep() anymore:
char *strsep(char...
|
by: plmanikandan |
last post by:
Hi,
I need to split the value stored in a string and store them to another
charrecter array.I am using strtok function.But i am getting invalid
output when there is no value between delimiter
...
|
by: Michael |
last post by:
Hi,
I have a proble I don't understand when using strtok(). It seems that if I
make a call to strtok(), then make a call to another function that also
makes use of strtok(), the original call is...
|
by: Amit Gupta |
last post by:
Hi -
I get a seg-fault when I compile and run this simple program.
(seg-fault in first call to strtok). Any clues?
My gcc is "gcc version 4.1.1 20070105 (Red Hat 4.1.1-51)"
#include...
|
by: weidongtom |
last post by:
Hi,
I've written the code that follows, and I use the function add_word(),
it seems to work fine
*before* increase_arrays() is called that uses realloc() to allocate
more memory to words. But...
|
by: Lothar Behrens |
last post by:
Hi,
I have selected strtok to be used in my string replacement function.
But I lost the last token, if there is one.
This string would be replaced
select "name", "vorname", "userid",...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |