473,378 Members | 1,236 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,378 software developers and data experts.

direct string manuplation not working

Hi All

Here is a program which basically tokenizes a string based on space
seperation.
But it does not run bcoz i am directly trying to manuplate the string.
I cannot use standard string functions due to performance issues

can anybody figure out whats gone wrong with this

#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define SP ' '
char **tokenize(char *);
char ** tokenize(char *ch)// to tokenize the string based on space
{
int count = 0;
char *mrk = ch;
while(*ch != '\0')
{
if(*ch == SP)
count++;
ch++;
}
count = count + 2;
ch = mrk;
char **c = (char **)malloc(count * sizeof(ch));
int count2;
char *fst,*lst;
fst = lst = ch;
for (count2 = 0; count2 < count - 1; count2++)
{
lst = strchr(lst,SP);
*lst = '\0'; //the DDD crashes at this point
lst++;
c[count2] = fst;
fst = lst;
}
c[count - 1] = NULL;
return c;

}
int main()
{

char *s = "hello how do u do";
char **t = tokenize(s);
printf("the str is %s\n",*t[0]);//similarly for s[1] & s[2]

}

Nov 29 '05 #1
9 1563
shyam wrote:
Hi All

Here is a program which basically tokenizes a string based on space
seperation.
But it does not run bcoz i am directly trying to manuplate the string.
I cannot use standard string functions due to performance issues

can anybody figure out whats gone wrong with this

#include<stdio.h>
#include<string.h>
#include<malloc.h> Non-standard header. malloc() is declared in:
#include <stdlib.h>

#define SP ' '
char **tokenize(char *);
char ** tokenize(char *ch)// to tokenize the string based on space
{
int count = 0;
char *mrk = ch;
while(*ch != '\0')
{
if(*ch == SP)
count++;
ch++;
}
count = count + 2;
ch = mrk;
char **c = (char **)malloc(count * sizeof(ch)); There's no need to cast the return value of malloc(), doing so can mask
errors. Better:
char **c = malloc(count * sizeof *c);

int count2;
char *fst,*lst;
fst = lst = ch;
for (count2 = 0; count2 < count - 1; count2++)
{
lst = strchr(lst,SP);
*lst = '\0'; //the DDD crashes at this point Right. `lst' points somewhere into the array of chars passed to the
function. In this case (see main() below) it is a string literal which
is not guaranteed to be modifiable.

lst++;
c[count2] = fst;
fst = lst;
}
c[count - 1] = NULL;
return c;

}
int main()
{

char *s = "hello how do u do" Try:
char s[] = "hello how do u do";

[BTW, you had a missing semicolon above. Cut and paste *real* code in
the future.]
;

char **t = tokenize(s);
printf("the str is %s\n",*t[0]);//similarly for s[1] & s[2]

}

Next time:
1) Post *real* code.
2) Get rid of all those absolutely useless blank lines.

Also, be aware that there may be (and most likely are) further bugs in
your code; there's just no way I'm going to go through it with a fine
toothed comb when presented like this.

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Nov 29 '05 #2
On 28 Nov 2005 18:02:46 -0800, "shyam" <sh********@gmail.com> wrote:
Hi All

Here is a program which basically tokenizes a string based on space
seperation.
But it does not run bcoz i am directly trying to manuplate the string.
I cannot use standard string functions due to performance issues

can anybody figure out whats gone wrong with this

#include<stdio.h>
#include<string.h>
#include<malloc.h>
Non-standard header. malloc and family are in stdio.h.


#define SP ' '
char **tokenize(char *);
char ** tokenize(char *ch)// to tokenize the string based on space
Why are you triple spacing between every line? Does your monitor
really show 300 lines or is there some reason you don't like to see as
much of your code as possible? However, you do get points for
indenting nicely.
{
int count = 0;
char *mrk = ch;
while(*ch != '\0')
{
if(*ch == SP)
count++;
ch++;
}
count = count + 2;
ch = mrk;
char **c = (char **)malloc(count * sizeof(ch));
Many pre-C99 systems do not tolerate definitions after executable
code.

Don't cast the return from malloc. It seldom helps and cause the
compiler to suppress a warning you really want to see.
int count2;
char *fst,*lst;
fst = lst = ch;
for (count2 = 0; count2 < count - 1; count2++)
{
lst = strchr(lst,SP);
*lst = '\0'; //the DDD crashes at this point
If you look in main, you will see that the argument you passed to this
function is a pointer to a string literal. This statement attempts to
change the contents of that literal. This is undefined behavior. An
immediate crash is one of the best types of undefined behavior.
lst++;
c[count2] = fst;
fst = lst;
}
c[count - 1] = NULL;
return c;
}

int main()
{
char *s = "hello how do u do";
char **t = tokenize(s);
printf("the str is %s\n",*t[0]);//similarly for s[1] & s[2]
The %s requires a char* for its argument. t is a char**; t[0] is a
char*; *t[0] is a char. I think you wanted t[0].

Don't you think your should print t[1], t[2], until you get to the
t[n] which is NULL.
}

<<Remove the del for email>>
Nov 29 '05 #3
On 2005-11-29, Barry Schwarz <sc******@oz.net> wrote:
Non-standard header. malloc and family are in stdio.h.


they're in what, now?
Nov 29 '05 #4
Jordan Abel wrote:
On 2005-11-29, Barry Schwarz <sc******@oz.net> wrote:
Non-standard header. malloc and family are in stdio.h.


they're in what, now?


The standard header, stdio.h, as he said. What's confusing you?

Brian

Nov 29 '05 #5
Default User:
Jordan Abel:
On 2005-11-29, Barry Schwarz <sc******@oz.net> wrote:
> Non-standard header. malloc and family are in stdio.h.


they're in what, now?


The standard header, stdio.h, as he said. What's confusing you?


Only if merely the first three letters are significant. :-)

Jirka
Nov 29 '05 #6
Default User wrote:
Jordan Abel wrote:
On 2005-11-29, Barry Schwarz <sc******@oz.net> wrote:
Non-standard header. malloc and family are in stdio.h.
they're in what, now?
The standard header, stdio.h, as he said. What's confusing you?

Are you sure about that, Brian? What's <stdlib.h> for, then?
Nov 29 '05 #7
On 2005-11-29, Default User <de***********@yahoo.com> wrote:
Jordan Abel wrote:
On 2005-11-29, Barry Schwarz <sc******@oz.net> wrote:
> Non-standard header. malloc and family are in stdio.h.


they're in what, now?


The standard header, stdio.h, as he said. What's confusing you?


the fact that thy're actually in stdlib.h, and, on my system at least,
including stdio.h does not cause declarations from stdlib.h to become
available.
Nov 29 '05 #8
Default User wrote:
Jordan Abel wrote:
On 2005-11-29, Barry Schwarz <sc******@oz.net> wrote:
Non-standard header. malloc and family are in stdio.h.

they're in what, now?


The standard header, stdio.h, as he said. What's confusing you?


The fact that malloc and friends are declared in stdlib.h and not
stdio.h possibly?
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 29 '05 #9
Jordan Abel wrote:
On 2005-11-29, Default User <de***********@yahoo.com> wrote:
Jordan Abel wrote:
the fact that thy're actually in stdlib.h, and, on my system at least,
including stdio.h does not cause declarations from stdlib.h to become
available.


Damn it. Sorry.
Brian
Nov 29 '05 #10

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

Similar topics

3
by: Jan Bols | last post by:
I'm using PHP 4.3 and APACHE2.0. I have a website that requires people to log in before they can download files from my website. A person is logged in if there is a session-variable $logged_in set...
4
by: __PPS__ | last post by:
Hello, I have a function that does some decoding (same as urldecode in php) and it returns std::string std::string urldecode(const char* in, std::size_t length); so, this function parses input...
22
by: Rich | last post by:
I am trying to create a site that will re-direct a user based on their OS. For this site I will need to send NT4 and 95 users to site A and 2000/XP users to site B. All others should be directed...
7
by: Randell D. | last post by:
Folks, I have a Javascript performance question that I might have problems explaining... In PHP, better performance can be obtained dealing directly with a variable, as opposed to an element...
1
by: acrocker | last post by:
I would like to provide access to users who need to be taken directly to the relevant page without further navigation instructions. Unfortunately the page I want to access is built using a...
10
by: Alex Greem | last post by:
Dear all, Our database (DB2 Workgroup 7.2 FP12) is constantly under heavy load. Most time CPU usage (1 Pentium3 1Ghz) is more 50% busy. We have 3GB RAM memory Our normal workload is 200-300...
1
by: faktujaa | last post by:
Hi ALL Please check the code below. OpenPrinter method returns Access is Denied error. Serial Printer is connected to the the IP address with name given at COM1 public enum ACCESS_MAS ...
1
by: SMichal | last post by:
Hi, how can I start new process with this kind of application ? C:\Direct.exe -a -b -c <C:\Script.src C:\log.txt As you can see...I'll start appliaction ConnectDirect with 5 paramaters ...
3
by: subramanian100in | last post by:
Consider the following program: #include <iostream> #include <string> using namespace std; class Test { public:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.