473,406 Members | 2,816 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,406 software developers and data experts.

parsing string into an array

I would like to parse a string into an array. I found on the net
the following codes which parse a string and print it. The result
is exactly what I want:

char * pch;
pch = strtok (buffer," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.");
}

To store the output to a string array, I created a two-dimensional
array and copied the output to the array:

char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output[i],pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n";i,output[i++]);
pch = strtok (NULL, " ,.");
}

This doesn't work. What's in the output array is nothing like the
output (on screen). How should it be done?
Nov 14 '05 #1
15 3595
In article <lb*********************@twister.southeast.rr.com> ,
John Smith <js****@company.com> wrote:
printf("output[%d] is %s.\n";i,output[i++]);


If I recall correctly, this uses undefined behaviour.
The post increment may be done at any time between the
sequence points, so it is not defined whether the first
reference to i has its value taken before or after the
increment.

It also is a syntax error to have the semicolon in the middle
of the statement. That line should not compile at all.
--
'ignorandus (Latin): "deserving not to be known"'
-- Journal of Self-Referentialism
Nov 14 '05 #2
Walter Roberson wrote:
In article <lb*********************@twister.southeast.rr.com> ,
John Smith <js****@company.com> wrote:

printf("output[%d] is %s.\n";i,output[i++]);

If I recall correctly, this uses undefined behaviour.
The post increment may be done at any time between the
sequence points, so it is not defined whether the first
reference to i has its value taken before or after the
increment.

It also is a syntax error to have the semicolon in the middle
of the statement. That line should not compile at all.


Thanks for the reply. That semicolon was a typo.

Actually, I had also tried the following (a separate i++) and it
did not work either.

char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output[i],pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n",i,output[i]);
i++;
pch = strtok (NULL, " ,.");
}
Nov 14 '05 #3


John Smith wrote:

Actually, I had also tried the following (a separate i++) and it
did not work either.

char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output[i],pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n",i,output[i]);
i++;
pch = strtok (NULL, " ,.");
}


The code provided here seems to be ok. What do you
mean by "did not work either".
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #4
John Smith wrote:
I would like to parse a string into an array. I found on the net
the following codes which parse a string and print it. The result
is exactly what I want:

char * pch;
pch = strtok (buffer," ");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,.");
}

To store the output to a string array, I created a two-dimensional
array and copied the output to the array:

char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output[i],pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n";i,output[i++]);
pch = strtok (NULL, " ,.");
}

This doesn't work. What's in the output array is nothing like the
output (on screen). How should it be done?

Smells like a buffer overflow to me.
Nov 14 '05 #5
John Smith wrote:
To store the output to a string array, I created a two-dimensional
array and copied the output to the array:

char output[5][55];
(snip)
This doesn't work. What's in the output array is nothing like the
output (on screen). How should it be done?


Are you aware that this will fail if:
* there are more than 5 "tokens"
* one or more of the tokens is (are) longer than 54 characters

That said, there are a couple more things to know:
* strtok() alters the string it works on. So you can't use
it to parse the same string twice unless you have made a copy
of this string before. This may be what happens to you if
your program tries both of your parsing attempts one after
the other on the same string.
* strtok() is one of those "evilish" C std library functions,
because it maintains an internal structure and thus cannot be
used in nested calls. So you must use it with a lot of care,
and it really is safe only in the simplest programs (unless
you really know what you're doing).
Not worth it in my opinion, since it's so easy to implement
"by hand" in a safe manner.

As a side note, I will add that outside of textbooks, multidimensional
arrays in C are not particularly useful. Their syntax is often
misleading (in my opinion) and they can serve only very limited
purpose. I tend to prefer arrays of arrays, which are easier to handle
and more flexible (in my opinion).
Nov 14 '05 #6
John Smith wrote:
Walter Roberson wrote:
In article <lb*********************@twister.southeast.rr.com> ,
John Smith <js****@company.com> wrote:
printf("output[%d] is %s.\n";i,output[i++]);

If I recall correctly, this uses undefined behaviour.
The post increment may be done at any time between the
sequence points, so it is not defined whether the first
reference to i has its value taken before or after the
increment.

It also is a syntax error to have the semicolon in the middle
of the statement. That line should not compile at all.

Thanks for the reply. That semicolon was a typo.

Actually, I had also tried the following (a separate i++) and it
did not work either.

char output[5][55];
char * pch;
int i=0;
pch = strtok (str," ");
while (pch != NULL)
{
strcpy(output[i],pch);
printf ("%s\n",pch);
printf("output[%d] is %s.\n",i,output[i]);
i++;
pch = strtok (NULL, " ,.");
}


Hi John,
(Do us a favor. See if you can turn off the annoying MIME stuff)
Like Al, I can't see what "did not work". Of course, you have not shown
us what the original 'char *str = "...";' looks like, or where you get
it. Enquiring Minds want to know.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #7
Joe Wright wrote:
John Smith wrote:
Walter Roberson wrote:
John Smith <js****@company.com> wrote:

printf("output[%d] is %s.\n";i,output[i++]);

If I recall correctly, this uses undefined behaviour. .... snip ...
It also is a syntax error to have the semicolon in the middle
of the statement. That line should not compile at all.
Thanks for the reply. That semicolon was a typo.

Actually, I had also tried the following (a separate i++) and it
did not work either.

.... snip ...
(Do us a favor. See if you can turn off the annoying MIME stuff)

.... snip ...

I am among the first to bitch and moan about mime and html in
Usenet, but I see none in this thread. Has something stripped it
on the path from there to here?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #8
Thanks to those who replied to my queston. I appreciate your help.
The code did work. I wasn't aware strtok would change the original
string. That caused some confusion.

(Is MIME turned off this time? Hope it is.)
Nov 14 '05 #9
John Smith wrote:
Thanks to those who replied to my queston. I appreciate your help.
The code did work. I wasn't aware strtok would change the original
string. That caused some confusion.

(Is MIME turned off this time? Hope it is.)


I can't tell about MIME anymore. After I wrote my note to you I've set
Thunderbird to ignore it.

--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #10
CBFalconer wrote:
Joe Wright wrote:

... snip ...
(Do us a favor. See if you can turn off the annoying MIME stuff)


... snip ...

I am among the first to bitch and moan about mime and html in
Usenet, but I see none in this thread. Has something stripped it
on the path from there to here?


I suspect you have turned it off in your reader, as I now have.
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #11
In article <42***************@yahoo.com>,
CBFalconer <cb********@worldnet.att.net> wrote:
I am among the first to bitch and moan about mime and html in
Usenet, but I see none in this thread. Has something stripped it
on the path from there to here?


It isn't the typical obvious MIME so my reader (trn) didn't notice
it either. He did, though, have a Content-type: text/plain
header that specified a charset of Big5. Content-Type is a MIME
header, and on some systems the Big5 (i.e., one of the Chinese
encodings) would trigger different behaviour than if it were
(say) 8859-1 .
--
Studies show that the average reader ignores 106% of all statistics
they see in .signatures.
Nov 14 '05 #12
Guillaume <"grsNOSPAM at NOTTHATmail dot com"> writes:
John Smith wrote:
To store the output to a string array, I created a two-dimensional
array and copied the output to the array:

char output[5][55];
(snip)
This doesn't work. What's in the output array is nothing like the
output (on screen). How should it be done?
[snip] As a side note, I will add that outside of textbooks, multidimensional
arrays in C are not particularly useful. Their syntax is often
misleading (in my opinion) and they can serve only very limited
purpose. I tend to prefer arrays of arrays, which are easier to handle
and more flexible (in my opinion).


I'm not certain what distinction you're making here. The declaration
char output[5][55];
defines output as an array of arrays of char, which is the closest
thing C has to a multi-dimensional array.

Perhaps you're thinking of an array of pointers to arrays?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #13
John Smith <js****@company.com> spoke thus:
(Is MIME turned off this time? Hope it is.)


It seems not to be - the header that Walter referred to is still
present:

Content-Type: text/plain; charset=Big5

although it also looks like the people it annoys have been able to
change their newsreader configuration, so you may not have to worry
about it (tin has not complained about it here, FWIW).

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #14
Christopher Benson-Manica wrote:
John Smith <js****@company.com> spoke thus:
(Is MIME turned off this time? Hope it is.)


It seems not to be - the header that Walter referred to is still
present:

Content-Type: text/plain; charset=Big5

although it also looks like the people it annoys have been able to
change their newsreader configuration, so you may not have to worry
about it (tin has not complained about it here, FWIW).


There is no attached mime section, nor any html. So I see no harm
in it, although I am one of the most vocal html/mime haters
extant.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #15

Christopher Benson-Manica wrote:
John Smith <js****@company.com> spoke thus:
(Is MIME turned off this time? Hope it is.)


It seems not to be - the header that Walter referred to is still
present:

Content-Type: text/plain; charset=Big5

although it also looks like the people it annoys have been able to
change their newsreader configuration, so you may not have to worry
about it (tin has not complained about it here, FWIW).

All it does for me is change the font from the default to a sans-serif
one. It's annoying to a certain extent, but not a killer. I haven't
figured out how to make XanaNews ignore it, if even possible.

Brian

Nov 14 '05 #16

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

Similar topics

4
by: ralphNOSPAM | last post by:
Is there a function or otherwise some way to pull out the target text within an XML tag? For example, in the XML tag below, I want to pull out 'CALIFORNIA'. ...
0
by: Marc van Boven | last post by:
I'm stuck with the following problem: My nusoap-client calls a server-function giveCombination(). The function giveCombination should return something like array( => array( 'a_id' => 6,...
6
by: Ulrich Vollenbruch | last post by:
Hi all! since I'am used to work with matlab for a long time and now have to work with c/c++, I have again some problems with the usage of strings, pointers and arrays. So please excuse my basic...
1
by: Andre Ranieri | last post by:
I'm having trouble programatically inserting an Excel file into an Image column in our CRM package's SQL 2000 database. The function appears to work ok, but when I attempt to access the file through...
12
by: Klaus Alexander Seistrup | last post by:
Hi group, I am new to xgawk (and seemingly to xml also), and I've been struggling all afternoon to have xgawką parsing an XHTML file containing a hCard˛, without luck. I wonder if you guys...
9
by: Paulers | last post by:
Hello, I have a log file that contains many multi-line messages. What is the best approach to take for extracting data out of each message and populating object properties to be stored in an...
2
by: RG | last post by:
I am having trouble parsing the data I need from a Serial Port Buffer. I am sending info to a microcontroller that is being echoed back that I need to remove before I start the actual important...
1
nine72
by: nine72 | last post by:
Ok, I am at a complete loss on this and have finally come to the XML Parsing Gods (and perhaps a PHP minor deity) for guidance… I will try my best to describe what I have going on… 1) I have 15...
9
nine72
by: nine72 | last post by:
Ok, so I have figured out how to parse an custom returned XML doc (actually a string that I made into a doc for testing). At this point I am attempting to integrate my parse routine into my main...
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.