LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-04-2002, 01:48 AM   #1
bobthebat
Member
 
Registered: Apr 2001
Location: Portland, OR
Distribution: Slackware 8
Posts: 82

Rep: Reputation: 15
Unhappy oh the pain, the pain of c


I need to translate this simple perl line into C:
@array = split(/:/, $string);
I know I'm going to need to use strtok, but my problem is that I don't know how big array has to be until I tokenize string, so I can't declare array because I don't know what size to make it. Do I have to parse it twice to find the number of tokens first and to store them second? This seems really inelegant, and I'm sure there's a better way. Can anybody help?
Thanks,
btb
 
Old 07-04-2002, 06:09 AM   #2
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
Well your options would be to:

* make an array which is always large enough
* count how many you would need first (probably would need to parse twice)
* use an array which can dynamically resize itself (if you are using c you will have to build your own implementation of that)
 
Old 07-04-2002, 11:30 AM   #3
biosx
Member
 
Registered: Jul 2002
Location: Chicagoland
Distribution: Gentoo, Ubuntu
Posts: 63

Rep: Reputation: 15
I'm sure you can get creative with the malloc() and realloc() functions. Check them out. I say you break it down like this:

1. malloc() out some memory enough to hold a good amount of data.

2. Create a function that walks through the string and counts the number of words found (excluding the colon-delimiters).

3. Then take that and check if you have to allocate more space.

4. If you need more space, do a realloc() and proceed with your tokenizing.

Good luck
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorry to be a pain, but can someone help please? twirl Programming 5 09-06-2005 05:15 PM
lselinux: pain in the @$$ chrisknight Linux - Software 5 08-25-2004 11:51 AM
What a pain BajaNick General 4 08-30-2003 11:15 PM
The Pain... crichards General 11 02-22-2003 05:19 AM
More pain than its worth?? ewaltonl Linux - General 7 03-06-2001 05:10 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration