I'm having problems where I need to parse a command line, and place
each token from strtok() into an array. I've read here: http://www.phim.unibe.ch/comp_doc/c_...PT/arrays.html (at
bottom) that you can access a char[][] by doing char[i] and getting the
string. However when I try the code below I get
error: invalid use of array with unspecified bounds
whenever argv is accessed. It is declared in main() as:
char argv[ARGV_LENGTH][ARGV_LENGTH]; /* ARGV_LENGTH is a const int of
64 */
This is pretty much my first time with C, I'm usually using Java and a
little C++. Any help is appreciated.
Thanks,
Mike
void parse_cmdline(c har cmdline [], char argv [][])
{
char * command;
int i = 1;
command = strtok(cmdline, DELIMITERS);
argv[0] = malloc(sizeof(c har)*(strlen(co mmand)+1));
strcpy(argv[0],command);
while (command != NULL) {
printf("%s\n", command);
command = strtok(NULL, DELIMITERS);
argv[i] = malloc(sizeof(c har)*(strlen(co mmand)+1));
strcpy(argv[i],command);
i++;
}
}
Nov 15 '05
11 8983
Netocrat wrote: On Wed, 19 Oct 2005 10:47:46 +0530, Vimal Aravindashan wrote:
Netocrat wrote:
The original while loop had the correct semantics. This do loop fails to check for the above function returning NULL. There is nothing wasted - it is a necessary check.
Check the OP's code again. He doesn't check for the first one himself.
Here is the relevant portion of the OP's code:
command = strtok(cmdline, DELIMITERS); ... while (command != NULL) { ... command = strtok(NULL, DELIMITERS); ... }
This has correct semantics; the do loop does not.
Perhaps you misread my comments as applying to the test for i < ARGV_LENGTH that I added.
I didn't misread it. Forget about what maybe relevant or not. The
following is this OP's original post:
command = strtok(cmdline, DELIMITERS);
argv[0] = malloc(sizeof(c har)*(strlen(co mmand)+1));
strcpy(argv[0],command);
while (command != NULL) {
As per the above code, even if strtok() returns NULL for the first call,
the OP makes it a point to call strcpy() for it.
Netocrat, I don't think either of us can decide what the "correct
semantics" are for the OP solution. I am surprised that he hasn't
commented on our replies, because apparently he wants a NULL at the end
of the arg list, which neither of our codes do. Given the OP's code, I think its safe to assume further that his set of DELIMITERS combined with strtok() will yield the necessary results for him.
It will likely generate correct results without ill-effect for the OP's situation, but a general warning re strtok is warranted.
Mike, you've been warned.
Cheers,
Vimal.
--
"If you would be a real seeker after truth, it is necessary that at
least once in your life you doubt, as far as possible, all things."
-- Rene Descartes
On Wed, 19 Oct 2005 17:19:58 +0530, Vimal Aravindashan wrote: Netocrat wrote: On Wed, 19 Oct 2005 10:47:46 +0530, Vimal Aravindashan wrote:Netocrat wrote:
The original while loop had the correct semantics. This do loop fails to check for the above function returning NULL. There is nothing wasted - it is a necessary check.
Check the OP's code again. He doesn't check for the first one himself. Here is the relevant portion of the OP's code:
command = strtok(cmdline, DELIMITERS); ... while (command != NULL) { ... command = strtok(NULL, DELIMITERS); ... }
This has correct semantics; the do loop does not.
Perhaps you misread my comments as applying to the test for i < ARGV_LENGTH that I added. I didn't misread it. Forget about what maybe relevant or not. The following is this OP's original post:
command = strtok(cmdline, DELIMITERS); argv[0] = malloc(sizeof(c har)*(strlen(co mmand)+1)); strcpy(argv[0],command); while (command != NULL) {
As per the above code, even if strtok() returns NULL for the first call, the OP makes it a point to call strcpy() for it.
He does and that is unsafe - strlen and strcpy do not take NULL pointers.
The original code's looping is not entirely correct but with those two
lines removed it is - perhaps my omitting them was misleading; the point
is that the while loop is more naturally suited to correctly dealing with
errors.
For the do loop to behave correctly one would have to either:
a) only enter the loop if cmdline != NULL - but why write the code for
that additional test when the while loop code already provides it?
b) eliminate the call to strtok prior to the loop, reverse the order of
the functions within the loop and break if strok returns NULL - but then
to remove unnecessary tests as you seemed to want to do, we should remove
the loop conditional - so a do loop is no more preferable than a
while(1){} or for(;;){} loop and the code is considerably different to
that originally proposed.
Netocrat, I don't think either of us can decide what the "correct semantics" are for the OP solution.
True, but we can decide when a solution fails to prevent NULL from being
passed to functions to which it should not be passed.
I am surprised that he hasn't commented on our replies, because apparently he wants a NULL at the end of the arg list, which neither of our codes do.
Well that can be added immediately prior to the return if he does want it:
if (i == ARGV_LENGTH)
i--;
argv[i++] = NULL;
[...]
-- http://members.dodo.com.au/~netocrat This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jamey Cribbs |
last post by:
At the risk of calling my manhood into question, I humbly submit the
following little diddy (which is a blatant rip-off of a heart wrenching
melody of the '70s by that international superstar, Mary Macgregor):
To the tune of "Torn Between Two Lovers":
Torn between two languages, both of which are awesome tools
Lovin' both of you is breakin' all the rules
Torn between two incredibly awesome scripting languages, I'm
startin' to drool
|
by: SimonC |
last post by:
I'm looking to do something similar to a feature found on
Ticketmaster.com, where you select your seats at a venue, and then you
have two minutes in which to take or leave them.
QUESTION 1a. Inside (or just after) the same query that searches for
available seats, I need to SIMULTANEOUSLY mark those seats as "on
hold".
I've only read about, but not yet used MySQL transactions, and wonder
if this simultaneous "search-and-hold"...
|
by: John Grenier |
last post by:
Hi,
I have to determine the "standing" (WIN - TIE - LOSS) from
confrontations between two teams on a contest. The table matchResults
has fields cont_id, team_id and contest_result (int).
TABLE matchResults
cont_id team_id contest_result
1 1 3
1 2 5
|
by: Willem |
last post by:
Hi,
I have a newbie question: is it possible to make a search form in asp that
searches in two different databases (access)?
Willem
|
by: Hank1234 |
last post by:
Can I use one Data Adapter and one Command Builder to update amny tables?
Currently in my data adapter I query two tables and fill them into two tables
in a data set. When I make a change to a record in the second table and call
the update method of the data adapter the command builders update command
text is for the first table. Can the command builder handle two tables?
Code example:
Dim oCOnn As New SqlConnection("Data Source=.;" &...
| |
by: Matt K. |
last post by:
Hi there,
I have a form in an Access project that contains a subform which
displays the results of a query of the style "select * from
where = #a certain date#". In the main part of the form
the user can change the date, which will force a requery in the
subform to bring up records from the date selected.
My question is this... The query in the subform is a very simple one,
with only three fields being returned. In the interest of...
|
by: Jamie Pittman via AccessMonster.com |
last post by:
I have two tables with 5000 entries on them. One is based for regular time
with several variables example (employee name, date,time in and out, code,
customer, building) I have another table that works with overtime but has
the same feilds within the table. I want to be able to run a report that
combines the regular time and overtime for the warehouse and seperated by
each customer. I have tried join tables but I keep ending with, (if it...
|
by: Hi5 |
last post by:
Hi,
I am designing a database for a client in which It has a client table
including the followings:
1-Table Client
2-Table lookupcategory
3-Table Ctegory
|
by: Prabhudhas Peter |
last post by:
I have two object instances of a same class...
and i assigned values in both object instances (or the values can be taken
from databse and assigned to the members of the objects)...
Now i want to compare these two objects so that it will return true if both
object's members have the same value...
it is good if u can give me a single function or simple code snippet..
Thank U
--
Peter...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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,...
|
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...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |