LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-30-2007, 10:44 AM   #1
rubadub
Member
 
Registered: Jun 2004
Posts: 236

Rep: Reputation: 33
Arrays in C


Hi,
I'm after an efficient way of putting the results of strtok into an array and return it from a function. I don't want to use linked-lists, stacks, etc, just a plain array.

The way i'm doing it at the moment reallocates two sets of memory on every pass of strtok. The other way i've done it is to declare lot's (max) of memory and then to copy it into a resized array at the end.

Is there a simpler way?

p.s. Is there a function which tells you how many elements strtok will output (like in java)?
 
Old 07-30-2007, 02:26 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
I'd allocate the memory of length equal to the string length plus an index table (of resonable length, max strlen/strlen (delim) ) and put the subsequent results as new string, saving start of each of them in the index table.

You can use a loop of strstr() to find out how many elements will be returned.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Arrays baks Programming 2 03-19-2007 12:32 AM
Question about outputing arrays with pointers, then just arrays... RHLinuxGUY Programming 1 04-12-2006 05:40 AM
java + arrays dilberim82 Programming 3 02-12-2005 02:33 PM
Arrays in C++ kalleanka Programming 5 02-07-2004 12:27 AM
help with arrays leroy27336 Programming 6 01-20-2004 06:07 PM

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

All times are GMT -5. The time now is 09:30 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