problem with char*'s and changing individual chars in it

W

whiteboy

My assignment is to duplicate the strtok function. My problem is i get
"access violation" errors when I try to turn a single char in a char*,
as I should. My question is how do I get around this? I know you can
change individual chars in a char array, but don't know how to
implement it. This code is a work in progress and is by no means
complete. Here's my code so far.

#include <iostream>
using namespace std;

char *bk_strtoke(char *, const char *);

int main()
{
char *tokethis = "Hello, EE*L&,3^*801";
char *stoned = bk_strtoke(tokethis, "&*^");

system("PAUSE");
return 0;
}

char *bk_strtoke(char *s1, const char *s2)
{

char *next_token = NULL;

if (*s1 != NULL) static char where_to_start[] = s1;

for (; where_to_start == next_token; where_to_start++)
{
for (int i = 0; i < strlen(s2); i++)
if (*where_to_start != *(s2 + i) && i == 2)
{
next_token = where_to_start;
break;
}
}

for (;; where_to_start++)
{
for (int i = 0; i < strlen(s2); i++)
//access violation error on next line
if (*where_to_start == *(s2 + i)) *where_to_start = '\0';
}
}
 
M

Mike Wahler

whiteboy said:
My assignment is to duplicate the strtok function. My problem is i get
"access violation" errors when I try to turn a single char in a char*,
as I should. My question is how do I get around this? I know you can
change individual chars in a char array, but don't know how to
implement it. This code is a work in progress and is by no means
complete. Here's my code so far.

I didn't analyze your code enough to determine whether it
duplicates the functionality of the standard 'strtok()'
function, but your 'access violation' is probably occurring
because you're trying to modify a string literal, which will
produce 'undefined' behavior. Define or allocate an array of
characters instead of just a pointer pointing at a literal,
as you've done.
#include <iostream>
using namespace std;

char *bk_strtoke(char *, const char *);

int main()
{
char *tokethis = "Hello, EE*L&,3^*801";

char tokethis[] = "Hello, EE*L&,3^*801";

char *stoned = bk_strtoke(tokethis, "&*^");

system("PAUSE");
return 0;
}

char *bk_strtoke(char *s1, const char *s2)
{

char *next_token = NULL;

if (*s1 != NULL) static char where_to_start[] = s1;

for (; where_to_start == next_token; where_to_start++)
{
for (int i = 0; i < strlen(s2); i++)
if (*where_to_start != *(s2 + i) && i == 2)
{
next_token = where_to_start;
break;
}
}

for (;; where_to_start++)
{
for (int i = 0; i < strlen(s2); i++)
//access violation error on next line
if (*where_to_start == *(s2 + i)) *where_to_start = '\0';
}
}

I suggest you study a bit more and become familiar with
string literals, arrays, pointers, and how they differ
from one another.


-Mike
 
J

John Harrison

whiteboy said:
My assignment is to duplicate the strtok function. My problem is i get
"access violation" errors when I try to turn a single char in a char*,
as I should. My question is how do I get around this? I know you can
change individual chars in a char array, but don't know how to
implement it. This code is a work in progress and is by no means
complete. Here's my code so far.

#include <iostream>
using namespace std;

char *bk_strtoke(char *, const char *);

int main()
{
char *tokethis = "Hello, EE*L&,3^*801";
char *stoned = bk_strtoke(tokethis, "&*^");

This is wrong, you cannot modify a string literal. Change to

char tokethis[] = "Hello, EE*L&,3^*801";
char *stoned = bk_strtoke(tokethis, "&*^");

Now tokethis is an array, not a pointer to a string literal.
system("PAUSE");
return 0;
}

char *bk_strtoke(char *s1, const char *s2)
{

char *next_token = NULL;

if (*s1 != NULL) static char where_to_start[] = s1;

If this code is compiling then you have a broken compiler,
where_to_start must be declared outside of the if statement, if you want
to use it outside of the if statement.

static char* where_to_start;
if (*s1 != NULL) where_to_start = s1;
for (; where_to_start == next_token; where_to_start++)
{
for (int i = 0; i < strlen(s2); i++)
if (*where_to_start != *(s2 + i) && i == 2)

i == 2? That's not right.
{
next_token = where_to_start;
break;
}
}

for (;; where_to_start++)
{
for (int i = 0; i < strlen(s2); i++)
//access violation error on next line
if (*where_to_start == *(s2 + i)) *where_to_start = '\0';
}

This is an inifinite loop, where_to_start will just keep getting bigger
and bigger and you will eventually get an access violation.

Maybe you are smoking too much?

john
 
A

Aleksey Loginov

whiteboy said:
My assignment is to duplicate the strtok function. My problem is i get
"access violation" errors when I try to turn a single char in a char*,
as I should. My question is how do I get around this? I know you can
change individual chars in a char array, but don't know how to
implement it. This code is a work in progress and is by no means
complete. Here's my code so far.

mde... school homework?
#include <iostream>
using namespace std;

char *bk_strtoke(char *, const char *);

go to c.l.c
int main()
{
char *tokethis = "Hello, EE*L&,3^*801";
char *stoned = bk_strtoke(tokethis, "&*^");

system("PAUSE");
return 0;
}

char *bk_strtoke(char *s1, const char *s2)
{

char *next_token = NULL;

if (*s1 != NULL) static char where_to_start[] = s1;

which compiler eat this trash?

static char * where_to_start = NULL;
if ( s1!=NULL && *s1 != '\0' ) where_to_start = st;
for (; where_to_start == next_token; where_to_start++)

always false if "where_to_start!=NULL" else UB in the next inner loop
{
for (int i = 0; i < strlen(s2); i++)
if (*where_to_start != *(s2 + i) && i == 2)
{
next_token = where_to_start;
break;
}
}

for (;; where_to_start++)

infinity loop
{
for (int i = 0; i < strlen(s2); i++)
//access violation error on next line
if (*where_to_start == *(s2 + i)) *where_to_start = '\0';

"where_to_start" goes beyond "s1" border
 
B

Bangalore

Hi,
As per my understanding , we cannot modify the string literal, but I
obersved strange behaviour in SGI machine.
When I tried, modifying string literal under SGI machine, it's working
fine without
any error or warning,
#include <iostream.h>
int main()
{
char *t="String";
*(t+4)='I';
cout << t << endl;
}
output:
StriIg

Can anyone explain why is it so?
Thanks
Bangalore
 
K

Karl Heinz Buchegger

Bangalore said:
Hi,
As per my understanding , we cannot modify the string literal, but I
obersved strange behaviour in SGI machine.
When I tried, modifying string literal under SGI machine, it's working
fine without
any error or warning,
#include <iostream.h>
int main()
{
char *t="String";
*(t+4)='I';
cout << t << endl;
}
output:
StriIg

Can anyone explain why is it so?

Undefined behaviour.

That means: Anything can happen.
And that includes: It works or seems to work as expected.
 
D

Default User

Aleksey Loginov wrote:

go to c.l.c


I'm sure they'd be thrilled to see <iostream> and "using namespace"
there.

The strtok() function is just as much part of C++ as it is of C. The
solutions for emulating it will possibly differ between the languages.




Brian
 
A

Aleksey Loginov

Default said:
Aleksey Loginov wrote:




I'm sure they'd be thrilled to see <iostream> and "using namespace"
there.


The strtok() function is just as much part of C++ as it is of C. The
solutions for emulating it will possibly differ between the languages.

as printf()/malloc() and other... :)
 
D

Default User

Aleksey said:
i didn't saw any usage "<iostream>" or "namespace std" in his sources
:)

Tsk Tsk, and you posting from Google where it's so easy to check.
Here's a snippet of the original post:


======================================================================
My assignment is to duplicate the strtok function. My problem is i get
"access violation" errors when I try to turn a single char in a char*,
as I should. My question is how do I get around this? I know you can
change individual chars in a char array, but don't know how to
implement it. This code is a work in progress and is by no means
complete. Here's my code so far.

#include <iostream>
using namespace std;

char *bk_strtoke(char *, const char *);

int main()
{
char *tokethis = "Hello, EE*L&,3^*801";
char *stoned = bk_strtoke(tokethis, "&*^");

system("PAUSE");
return 0;
}

======================================================================



Brian
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top