Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 11-29-2010, 07:50 PM   #1
Senior Member
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: SlackWare > Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie stretch buster
Posts: 1,620

Rep: Reputation: 97
C: sscanf but number of variables unknown


assign integers into array form string.

From string
"123,456,789,0123,4567,8901" (comma separated 6 integer numbers)
To array
a[0] = 123; a[1] = 456 ....a[5] = 8901

Idea is sscanf(string,"%d%d%d...",&(a[0]),&(a[1]),&(a[2])...)

Problem is that array size is unknown until the string is available for scanning. It could be huge.

What I thought:
Sounds like file scanning, it is easy. But, scanning same string gives me same value.

Umm.. vscanf or something else would do, once format string is determined. Yes, the format string can be made without any trouble. but how to pass variable??

What I did:
scan one variable, then manipulate string to clear fields from top to comma, scan next variable. (loop)

My way works, however, is there any smarter way?

Happy Penguins!
Old 11-29-2010, 07:57 PM   #2
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Personally, I'd vote "for loop".

Or maybe implement your own list or vector struct (since it sounds like you're programming in C, and can't use an STL container).

But if you want a function that takes a variable number of arguments (and, of course, if you know the #/arguments before you call the function), then "stdarg" is what you're looking for.
Old 11-30-2010, 02:52 AM   #3
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
The canonical way would be to use strtok() and then atoi().


ansi-c, scan, string

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
Counting the number of exit variables - Bash. 0bfuscated Programming 3 07-02-2010 11:43 AM
C - How to initialize an undetermined number of variables golmschenk Programming 11 02-07-2010 11:25 AM
C Syntax - Passing in unknown number of arguments? CoderMan Programming 2 05-18-2009 04:41 AM
ftp: connect :Unknown error number chobo Linux - Newbie 10 07-17-2008 06:09 AM
Using sscanf to grab an unknown number of variables R00ts Programming 4 06-06-2005 05:03 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:07 AM.

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