473,796 Members | 2,434 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3631
In article <lb************ *********@twist er.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************ *********@twist er.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******@myrapi dsys.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, multidimensiona l
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************ *********@twist er.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.c om, 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

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

Similar topics

4
2660
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'. <txtNameUSState>CALIFORNIA</txtNameUSState>
0
6473
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, 'b_id' => 9, 'c_id' => 3,
6
2121
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 question: I want to parse a string like "3.12" to get two integers 3 and 12. I wanted to use the function STRTOK() I wrote a main- and a subfunction like: main() {
1
13299
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 the application's front end the file appears to be corrupt. The front-end application has a way of inserting files to the column, however when I analyze this in SQL Profiler the contents of the Byte array appear to be quite different than the one...
12
2562
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 could give me a push... Let's say I have the following XHTML file: #v+
9
1991
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 ArrayList? I have tried looping through the logfile using regex, if statements and flags to find the start and end of each message but I do not see a good time in this process to create a new instance of my Message object. While messing around with...
2
4887
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 data reading. For instance this is my buffer string: 012301234FFFFFFxFFFFFFxFFFFFFx Where the FFFFFF is my Hex data I need to read. I am using the "x" as a separater as I was having problems using the VbCrLf. But I think
1
2207
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 form pages, well over 500 potential fields, which are written in PHP. While most pages are one time entry forms, there are 5 that can be “recycled” as many times as needed. An example would be the Contacts Form. A user can give me 1 contact and move...
9
6284
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 code and I am having an issue getting it to mesh, and am looking for a little help in how to 1) combine the two and 2) how to catch the incoming xml string. FYI the string is sent to me as a return message to an xml message that I send first....
0
9680
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10455
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10173
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10006
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9052
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7547
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4116
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.