472,784 Members | 986 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,784 software developers and data experts.

convert string to pointer to pointer?

Hi i converted a program to a lib. The parameters of main are
maincommand(int argc, char **argv)
I want to use main a s function that takes a string how can i convert
a char * to a char ** or even better how should i substitute the parameters
for a string. thanks
Nov 14 '05 #1
4 9906

On Thu, 8 Jan 2004, Joe H wrote:

Hi i converted a program to a lib. The parameters of main are
maincommand(int argc, char **argv)
I want to use main a s function that takes a string how can i convert
a char * to a char ** or even better how should i substitute the parameters
for a string. thanks


How many parameters, how organized, etc.? For example, if you're
used to calling the old program at the command line like this:

% myprog 100
% myprog 42
% myprog -8192

then you might prototype the new library function as

int myprog(int arg);

But if you write big complicated programs that you call like this:

% myprog -X 310 -Y 100 -bc -g 5
% myprog -X 80 -Y 80 -b -g 10 -k1 1.5 -k2 .3
% myprog -c -k2 50

then you might be better off with a prototype that requires the client
to explicitly state each possible argument's value:

int myprog(int X, int Y, int g, double k1, double k2, int B, int C);

Finally, if you are into clever little parsing algorithms, you
might write programs that get called like this:

% myprog "echome"
% myprog -o foo bar
% myprog recursive -o foo

and then maybe it would make sense to let your library function do
its own splitting-up-of-arguments, and just take a single string
with all the arguments glommed together, like this:

int myprog(const char *all_the_arguments);

It really depends on what you're trying to do.

If you want to pass a single string to a 'main'-like function
that takes (int, char **), you can do it like this:

int callmain(const char *p)
{
int my_argc;
char *my_argv[100];

my_argc = 2;
my_argv[0] = "program name goes here, usually";
my_argv[1] = (char *)p; /* EVIL CAST! EVIL EVIL EVIL! */
my_argv[2] = NULL;
return fake_main(my_argc, my_argv);
}

Notice the presence of an EVIL EVIL EVIL cast to (char *). That's
only necessary because I made 'callmain' take a (const char *)
parameter. I did that only because it seems nicer to the user. It
works as long as 'fake_main' behaves itself and doesn't go modifying
'p' behind our backs. Maybe it would be better to remove the 'const'
altogether... it depends.

HTH,
-Arthur

Nov 14 '05 #2
joe
thanks for your reponse ill try to make sense out of that
the program takes the pointer to pointer and the int and gets its
parameters like this

while ((ch = getopt(argc, argv,
"b:c:Dde:f:F:hHk:lmn:o:pPr:sS:tT:uUv:x:z:")) != -1)

i would like to give it a string rather than the pointer to pointer. i have
done this in the main program and it compiles but i cant get the function
to run

#include "zib.h"
int main(int argc, char* argv[])
{
char line[100] = "-s -T 1002\n";

int t = zcommand(1, &line);

return 1;

}

Nov 14 '05 #3

On Thu, 8 Jan 2004, joe wrote:

thanks for your reponse ill try to make sense out of that
the program takes the pointer to pointer and the int and gets its
parameters like this

while ((ch = getopt(argc, argv,
"b:c:Dde:f:F:hHk:lmn:o:pPr:sS:tT:uUv:x:z:")) != -1)
'getopt' is not a standard C function, and so it's off-topic here.
That's not just zealous topicality guidance, either -- I really have
no idea how 'getopt' works, and I don't care. But I can make some
assumptions, most of them implicit, and let you puzzle out what works
and what doesn't.
i would like to give it a string rather than the pointer to pointer. i have
done this in the main program and it compiles but i cant get the function
to run int main(int argc, char* argv[])
{
char line[100] = "-s -T 1002\n";

int t = zcommand(1, &line);


Where 'zcommand' is the old 'main' function, no doubt. Okay, I'm
not surprised that it didn't work. I recommend that you take a good
look at the last public C draft standard, N869 (which you can Google
for; it comes in plain text as n869.txt[.gz]). It has a section
somewhere detailing exactly what the implementation guarantees to
be present in 'argc' and 'argv' -- and thus what *you* need to
guarantee will be present in *your* "fake" argc and argv.
I would quote N869 right here, but I'm on a slow connection this
week. Maybe someone else will. Anyway, search the file for the
words 'argc' and 'argv' and I bet you'll find it.

You [probably] need to split up those arguments in that string
literal, like this [but I don't know how lenient 'getopt' might
be on your platform -- maybe it's a non-issue]:

char *line[] = {"dummy", "-s", "-T", "1002", NULL};

int t = zcommand(4, line);

If this *is* the issue [consult your 'getopt' documentation, such
as "man getopt" or your compiler manual], then you might need to
write some code to split up the "command-line arguments" yourself.
That is, a function that can take the input
"-s -T 1002\n"
and produce the output
{"dummy", "-s", "-T", "1002", NULL}

If it turns out you do need such a function, check Google for
"command line argument parsing" or post to a group like
comp.sources.wanted. (comp.lang.c is *not* the place to ask other
people to write whole big functions for you.)

HTH,
-Arthur
Nov 14 '05 #4
joe


thanks alot, that was great
Nov 14 '05 #5

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

Similar topics

7
by: Bob Rock | last post by:
Hello, converting from the managed to the unmanaged world (and viceversa strings) and byte arrays is something I do often and I'd like to identify the most correct and efficient way to do it....
8
by: ppcdev | last post by:
Here's what I try : LPCTSTR tst = (LPCTSTR) (LPCWSTR) Marshal::StringToHGlobalUni(str); c:\MyNetPrj\Prj0001\stunt.cpp(244): error C2440: 'type cast' : cannot convert from 'System::IntPtr' to...
4
by: Al Reid | last post by:
I have a simple function that I use in many of my applications. It allows one to update the "Status" panel of a status bar and optionally set the MousePointer. This is useful if the status is...
3
by: kaizen | last post by:
Hi, i wrote the code in C and compiled in VC++ compiler. at that time it has thrown the below mentioned error. error C2664: 'strcpy' : cannot convert parameter 2 from 'char' to 'const char *'...
10
by: rodrigo.gloria | last post by:
I am trying to convert a integer to an address of a function pointer. I want to encrypt the pointer and then do some validation, afterwards i will decrpyt the pointer back to an address. Can...
5
by: jeremito | last post by:
I am extending python with C++ and need some help. I would like to convert a string to a mathematical function and then make this a C++ function. My C++ code would then refer to this function to...
5
by: Gary Wessle | last post by:
Hi is there a way to convert a string to a function name and fire it. like void his_fun(){ cout << "his is here" << endl; } vector<stringvec; vec.push_back("his");
4
by: meendar | last post by:
Hi, I am having a character pointer which contains ascii values. i just want to convert all these ascii values to respective characters and again store it in another character pointer. ...
4
by: Neal Becker | last post by:
In an earlier post, I was interested in passing a pointer to a structure to fcntl.ioctl. This works: c = create_string_buffer (...) args = struct.pack("iP", len(c), cast (pointer (c),...
14
by: rtillmore | last post by:
Hello, I did a quick google search and nothing that was returned is quite what I am looking for. I have a 200 character hexadecimal string that I need to convert into a 100 character string. ...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.