473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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';
}
}

Sep 20 '05 #1
8 1592

"whiteboy" <bj*****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
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
Sep 20 '05 #2
whiteboy wrote:
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
Sep 21 '05 #3

whiteboy wrote:
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
}
}


Sep 21 '05 #4
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

Sep 21 '05 #5
Bangalore wrote:

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.
--
Karl Heinz Buchegger
kb******@gascad.at
Sep 21 '05 #6
Aleksey Loginov wrote:

#include <iostream>
using namespace std;

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


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
Sep 21 '05 #7

Default User wrote:
Aleksey Loginov wrote:

#include <iostream>
using namespace std;

char *bk_strtoke(char *, const char *);
go to c.l.c

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


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

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... :)

Sep 22 '05 #8
Aleksey Loginov wrote:

Default User wrote:
Aleksey Loginov wrote:

> #include <iostream>
> using namespace std;
>
> char *bk_strtoke(char *, const char *);

go to c.l.c

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


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
Sep 22 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ohaya | last post by:
Hi, I'm a real newbie, but have been asked to try to fix a problem in one of our JSP pages that is suppose to read in a text file and display it. From my testing thus far, it appears this page...
12
by: Francis Bell | last post by:
Hello, I've got a program that is reading in a data file of 25 lines. Here is an example of the first two lines: sp/spinnerbait/AAA Lures/Mad Phil/silver/bass/1/1 f/floating minnow/AAA...
15
by: Gregg Woodcock | last post by:
My compiler (m68k-gcc) does not allow this (simplified example; not my real code) and I don't see why not: { char *arrayCharPtr1 = {"a", "ab", NULL}; char *arrayCharPtr2 = {"A", "AB", NULL};...
1
by: rir3760 | last post by:
Since a few days ago I have been working with the program I post below (a school assignment). The purpose of the program is to work with the va_ macros (stdarg.h) and arrays of arrays, hopefully...
4
by: Ori :) | last post by:
Hi guys I have a 50 charecters long string and I need to examine every charectar individually, I thought of converting the string into and Char array of 50 and then go over the array, how can I...
7
by: owolablo | last post by:
Can anybody please tell me how to change the individual elements of a char variable. I need to parse through the string, check for a particular character and change it to something else if it is...
9
by: anon.asdf | last post by:
In terms of efficieny: Is it better to use multiple putchar()'s after one another as one gets to new char's OR is it better to collect the characters to a char-array first, and then use...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
4
by: HansWernerMarschke | last post by:
I hate pointers. I want to copy something from one memory position to another memory position. Both strings overlap. // The rotor type typedef struct { int wheel_number; char *wheel; char...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.